【发布时间】:2014-02-25 10:49:28
【问题描述】:
我有一个小型应用程序(HTML 表单、作为控制器的 servlet 和 jsp 文件),我试图弄清楚为什么我不能将请求从 servlet 转发到 jsp 文件。
问题是从 html 提交后,显示“HTTP Status 404”
申请流程:
- 从 html 提交。
- 控制器从 html 中获取名称。
- 控制器应该将请求移动到 jsp 文件。
谢谢!
项目层次结构: http://s23.postimg.org/kgt7r7lwb/Capture.jpg
main.html:
<html>
<title>Coupons categories</title>
<body>
<h1 align="center">Coupons categories</h1>
<form method="GET" action="Controller">
Select category
Type:
<select name="type" size=1>
<option value="restaurants">Restaurants</option>
<option value="electrics">Electrics</option>
<option value="hotels">Hotels</option>
</select>
<br><br>
<input type="Submit">
</form>
</body>
<html>
controller.java:
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//PrintWriter out = response.getWriter();
//out.write(request.getPathInfo());
String path = request.getParameter("type");
if(path.equals("electrics"))
{
request.setAttribute("timestamp", new Date());
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/view/electrics.jsp");
dispatcher.forward(request, response);
}
else if(path.equals("hotels"))
{
request.setAttribute("timestamp", new Date());
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/view/hotels.jsp");
dispatcher.forward(request, response);
}
else if(path.equals("restaurants"))
{
request.setAttribute("timestamp", new Date());
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/view/restaurants.jsp");
dispatcher.forward(request, response);
}
}
electrics.jsp:
<%@ page language="java" contentType="text/html; charset=windows-1255"
pageEncoding="windows-1255"%>
<!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=windows-1255">
<title>Insert title here</title>
</head>
<body>
<h2>products list...</h2>
<%
Object ob = request.getAttribute("timestamp");
out.println(ob);
%>
</body>
</html>
web.xml:
<description>
CouponsServer
</description>
<display-name>Controller for CouponsServer</display-name>
<servlet>
<servlet-name>Controller</servlet-name>
<servlet-class>uses.server.Controller</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Controller</servlet-name>
<url-pattern>/Controller</url-pattern>
</servlet-mapping>
</web-app>
更新: 可能问题出在controller.java 中。 当我尝试以下代码时,我得到了 HTTP 状态 500。 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.write(request.getPathInfo());
}
【问题讨论】:
-
如果 getRequestDispatcher 上的路径参数是绝对的,它仍然相对于 servlet 上下文(部署应用程序名称)。 '/view' 目录是应用程序的第一层目录吗?您还可以将相对路径与 getRequestDispatcher 一起使用,该路径相对于 Servlets 当前路径。
-
@user1257931 /查看 WebContet 中的文件夹。你可以看到层次结构:s23.postimg.org/kgt7r7lwb/Capture.jpg
-
将
main.html从view移动到WebContent时是否出现问题? -
@Pshemo 是的,仍然出现。
-
@user1257931 请在上面的问题中查看我的编辑。
标签: java html jsp servlets model-view-controller