我有一个 JSP 页面,其中包含一个文本区域和一个按钮、一个 servlet 和一个新的 jsp:
我的 3 个问题是:
- 我的analysis.jsp 是空的。我的浏览器打开一个新选项卡,但没有文本“Hello World”
- 我的java类中的变量“sql”也是空的。它应该包含我的文本区域中的文本
- 在我的变量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");
}
分析.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 不为空 <(^,^<) 但其他 2 个问题尚未解决:)
最佳答案
是的,这可能很简单
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
}
}
关于java - JSP将文本交给java和另一个jsp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35203460/