我有一个JSP页面,其中包含一个textarea和一个按钮,一个servlet和一个新的jsp:
我的三个问题是:
index.jsp
<form action="ServletName" method="post" target="_blank"> <div class="form-group"> <label for="codeEditor" style="margin-top: 15px;">Code:</label> <textarea name="codeEditor" class="form-control" id="codeEditor" rows="15" style="resize: none;"></textarea> </div> <div class="analysisButton"> <button type="submit" id="startAnalysis" class="btn btn-default btn-block" style="margin-top: 10px;">Start analysis</button> </div> </form>
ServletName.java
protected void doPost(...) ...{ String sql = request.getParameter("codeEditor"); response.sendRedirect("/analysis.jsp"); }
analysis.jsp
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page</title> </head> <body> <h1>Hello World!</h1> </body>
提前谢谢了
更新:没关系,变量sql不是空的<(^,^ <),但其他两个问题是打开的:)
是的,这可能很简单
JSP
<form action="some_url" method="post" > <inputarea name="sqlQuery" > <input type="submit" value="Sql query" > <form >
在您的servlet中,您将获得类似
Servlet
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { ...//check that the request is correct String query = request.getParameter("sqlQuery");//name of textarea try{ Connection conn = getConnection();//Search SO for how to get a connection PreparedStatement stmt = conn.prepareStatement(query); //if your query has any arguments(?) (e.g select * from tbl where id=?), then you should set them too //stmt.setInt(1, 1000); ResultSet rs = stmt.executeQuery(); while(rs.next()){ //get database dana here int someIntVal = rs.getInt("intColumn"); String someStr = rs.getString("someStringColumn"); }catch(SQLException e){ //handle exception } }