一尘不染

JSP将文本移交给Java和另一个jsp

jsp

我有一个JSP页面,其中包含一个textarea和一个按钮,一个servlet和一个新的jsp:

我的三个问题是:

  1. 我的analysis.jsp为空。我的浏览器打开了一个新标签,但没有文字“ Hello World”
  2. 我的Java类中的变量“ sql”也为空。它应该包含我的文本区域中的文本
  3. 我的变量sql不为空后(在分析之后)如何将新的新代码移交给新的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不是空的<(^,^ <),但其他两个问题是打开的:)


阅读 218

收藏
2020-06-10

共1个答案

一尘不染

是的,这可能很简单

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
}
}
2020-06-10