我在JSP中遇到了问题,我有以下servlet:
@SuppressWarnings("serial")
public class HelloAppIgorServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
RequestDispatcher disp = req.getRequestDispatcher("/mainpage.jsp");
disp.forward(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
PrintWriter pw = resp.getWriter();
pw.print("Test");
pw.close();
}
}
和一个名为mainpage.jsp的JSP文件:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<p>Hello there!<p>
<form action="." method="post">
<input type="text" name="name1" />
<input type="submit" value="name2" />
</form>
</body>
</html>
问题是doPost()方法不起作用。当我点击按钮时,它会将我重定向到index.html页面。我确定问题不在servlet中,所以它可以在哪里?
答案 0 :(得分:1)
您需要在<form action="." method="post">
中设置相应的操作。该操作是您在<servlet-mapping>
中通过web.xml
定义的servlet的(相对)URL。