JSP将文本移交给Java和另一个JSP [英] JSP hand over text to java and another jsp

查看:72
本文介绍了JSP将文本移交给Java和另一个JSP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a JSP page with a textarea and a button, a servlet and a new jsp:

我的3个问题是:

  1. 我的analysis.jsp为空.我的浏览器打开了一个新标签,但没有文字"Hello World"
  2. 我的java类中的变量"sql"也为空.它应该包含我的文本区域中的文本
  3. 如何在变量sql不为空(分析后)之后将新的新代码移交给新的jsp

index.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

ServletName.java

protected void doPost(...) ...{
    String sql = request.getParameter("codeEditor");

    response.sendRedirect("/analysis.jsp");
}

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不为空<((^,^<)),但其他两个问题都未解决:)

UPDATE: nevermind the variable sql isn't empty <(^,^<) but the other 2 questions are open :)

推荐答案

是的,这可能很简单

JSP

<form action="some_url" method="post" >
<inputarea name="sqlQuery"  >
<input type="submit" value="Sql query" >
<form >

在您的servlet中,您将拥有类似的东西

In your servlet, you'll have something like

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
}
}

这篇关于JSP将文本移交给Java和另一个JSP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆