当我在Eclipse上的index.jsp上运行我的Mavenproject时,它会打开它。我试图在这里打开我的AdressServlet槽/ EnterAddress,我得到了错误。
的index.jsp:
<html>
<body>
<h2>Welcome</h2>
<p>
We are going to get started with some question.
First we will need some information about you.
</p>
<a href="/EnterAddress">Start</a>
</body>
</html>
AddressServlet:
package Servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import Bean.SurveyBean;
import Service.SurveyService;
@WebServlet(value = "/EnterAddress", initParams = {
@WebInitParam(name = "addressPage",
value = "/WEB-INF/pages/Address.jsp"),
@WebInitParam(name = "QuestionURL", value = "Question") })
public class AddressServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public void init() throws ServletException{
super.init();
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
SurveyBean bean = new SurveyBean();
HttpSession sess = req.getSession();
sess.setAttribute("surveyBean", bean);
resp.sendRedirect("/pages/Address.jsp");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpSession sess = req.getSession();
SurveyBean bean = (SurveyBean) sess.getAttribute("surveyBean");
bean.setName(req.getParameter("name"));
bean.setStreet(req.getParameter("street"));
bean.setNumber(req.getParameter("number"));
bean.setZipcode(req.getParameter("zipcode"));
bean.setCity(req.getParameter("city"));
bean.setEmail(req.getParameter("email"));
sess.setAttribute("surveyBean", bean);
}
}
的web.xml:
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
</web-app>
在address.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>
<form method="POST">
Name: <input type="text" name="name"/>
Street: <input type="text" name="street"/>
Number: <input type="text" name="number"/>
Zipcode: <input type="text" name="zipcode"/>
email: <input type="text" name="email"/>
<input type="submit" value="OK"/>
</form>
</body>
</html>
我不知道为什么会收到错误。我没有在index.jsp上收到错误。 但是当我在de jsp上按Start时出现错误
当我做BalusC编辑的事情时它仍然不起作用我不知道我做错了什么或我如何解决它
答案 0 :(得分:3)
首先,这里有几点要记住:
通过在浏览器上键入localhost:8080/WEB-INF/foo.jsp
之类的内容,无法直接访问 WEB-INF 下的任何jsp文件。
servlet可以访问它们。
以下是您需要做的事情
将web.xml Doctype标记替换为以下内容:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
上述更改可确保注释适用于您的servlet。这可能不是一个直接问题,因为我不知道你是否可以访问servlet,返回404也可能是因为找不到jsp。
使用response.sendRedirect()
与在浏览器上键入jsp url相同,因为资源位于WEB-INF下,因此无法获取资源。路径 WEB-INF 甚至不包含在您的网址中
您将需要转发到servlet中的jsp,如下所示:
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/WEB-INF/pages/Address.jsp");
dispatcher.forward(request, response);