提前感谢您的帮助。我试图通过使用单选按钮对象从jsp获得答案,然后发送到servlet,该servlet将管理请求并发送到特定的servlet以影响数据库。我在管理请求的servlet中遇到问题。我无法将请愿书重新发送到最终的servlet。源代码的部分是下一个:
PolicyManager.jsp:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>Change the current policy</h1>
<form action="ManageRequest" method="Post">
Which action would you like to apply?<br><br>
<input type="radio" name="policy" value="add">Add<br>
<input type="radio" name="policy" value="delete">Delete
<br><br>
<input type="submit" value="submit"/>
</form>
</body>
</html>
ManageRequest.java:
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class ManageRequest
*/
@WebServlet("/ManageRequest")
public class ManageRequest extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public ManageRequest() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
PrintWriter printWriter = response.getWriter();
printWriter.print("doGet method");
String policy=request.getParameter("policy");
if (policy.equals("add") ) {
printWriter.println("<html><body>Redirecting to Add servlet section..."+"<br>");
RequestDispatcher dispatcher = request.getRequestDispatcher("/AddPolicy");
dispatcher.forward(request, response);
} else if (policy.equals("delete")) {
printWriter.println("<html><body>Redirectin to Delete servlet section..."+"<br>");
RequestDispatcher dispatcher = request.getRequestDispatcher("/DeletePolicy");
dispatcher.forward(request, response);
}
else{
printWriter.println("<html><body>A different option..."+"<br>");
}
printWriter.println("</body></html>");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
PrintWriter printWriter = response.getWriter();
String policy=request.getParameter("policy");
printWriter.print("doPost method");
if (policy.equals("add") ) {
printWriter.println("<html><body>Redirecting to Add servlet section..."+"<br>");
RequestDispatcher dispatcher = request.getRequestDispatcher("/AddPolicy");
dispatcher.forward(request, response);
} else if (policy.equals("delete")) {
printWriter.println("<html><body>Redirectin to Delete servlet section..."+"<br>");
RequestDispatcher dispatcher = request.getRequestDispatcher("/DeletePolicy");
dispatcher.forward(request, response);
}
else{
printWriter.println("<html><body>A different option..."+"<br>");
}
printWriter.println("</body></html>");
}
}
AddPolicy.java:
package PolicyManager;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class AddPolicy
*/
@WebServlet("/AddPolicy")
public class AddPolicy extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public AddPolicy() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
PrintWriter printWriter = response.getWriter();
printWriter.println("<h1>Add Policy</h1>");
printWriter.println("Add Policy");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
DeletePolicy.java:
package PolicyManager;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class DeletePolicy
*/
@WebServlet("/DeletePolicy")
public class DeletePolicy extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public DeletePolicy() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
PrintWriter printWriter = response.getWriter();
printWriter.println("<h1>Delete Policy</h1>");
printWriter.println("Delete Policy");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
web.xml:
<servlet>
<servlet-name>ManageRequest</servlet-name>
<servlet-class>PolicyManager</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ManageRequest</servlet-name>
<url-pattern>/PolicyManager</url-pattern>
</servlet-mapping>
问题是只有我可以获得必须将请愿重定向到最终servlet的管理器servlet。
感谢您的帮助。
干杯
答案 0 :(得分:1)
您应该尝试了解RequestDispatcher.include()
和RequestDispatcher.forward().
RequestDispatcher.forward()
表示请求处理的完整控制转发到另一个servlet。转发servlet不应该做任何事情,比如写响应或提交响应。如果响应在转发之前在调用servlet中提交,则抛出IllegalStateException
。如果响应尚未提交,则响应将被清除。
无论如何,当您转发到另一个servlet时,将某些内容写入响应是无效的。
如果您需要为各种servlet的响应写一些内容,则应使用RequestDispatcher.include().
在你的情况下,你在forward()之前写的东西可能是问题。
printWriter.println("<html><body>Redirecting to Add servlet section..."+"<br>
答案 1 :(得分:-1)
您必须添加: response.sendRedirect是( “http://localhost:8080/clientLibrary/DeletePolicy”);
而不是: RequestDispatcher dispatcher = request.getRequestDispatcher(“/ DeletePolicy”); dispatcher.forward(请求,响应);
AddPolicy也一样。
干杯