从servlet转发请求到jsp [英] Forward request from servlet to jsp
问题描述
我有一个小应用程序(HTML表单,servlet作为控制器和jsp文件),我试图找出为什么我不能将请求从servlet转发到jsp文件。
问题出在HTML提交后,出现HTTP状态404
应用程序流:
- 从html提交。
- 控制器从html获取名称。
- 控制器应该将请求移至jsp文件。
谢谢!
项目层次结构:
http://s23.postimg.org/kgt7r7lwb/Capture.jpg
main.html:
< html>
< title>优惠券类别< / title>
< body>
< h1 align =center>优惠券类别< / h1>
< form method =GETaction =Controller>
选择类别
类型:
< select name =typesize = 1>
< option value =restaurants>餐厅< / option>
< option value =electrics> Electrics< / option>
< option value =hotels>酒店< / option>
< / select>
< br>< br>
< input type =Submit>
< / form>
< / body>
< html>
controller.java:
@Override
保护无效doGet(HttpServletRequest请求,HttpServletResponse响应)抛出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 =javacontentType =text / html; charset = windows-1255
pageEncoding =windows-1255 %>
<!DOCTYPE html PUBLIC - // W3C // DTD HTML 4.01 Transitional // ENhttp://www.w3.org/TR/html4/loose.dtd\">
< html>
< head>
< meta http-equiv =Content-Typecontent =text / html; charset = windows-1255>
< title>在此插入标题< / title>
< / head>
< body>
< h2>产品清单...< / h2>
<%
Object ob = request.getAttribute(timestamp);
out.println(ob);
%>
< / body>
< / html>
web.xml:
< description>
CouponsServer
< / description>
< display-name> CouponsServer控制器< / display-name>
< servlet>
< servlet-name>控制器< / servlet-name>
< servlet-class> uses.server.Controller< / servlet-class>
< / servlet>
< servlet-mapping>
< servlet-name>控制器< / servlet-name>
< url-pattern> / Controller< / url-pattern>
< / servlet-mapping>
< / web-app>
更新:
可能问题出在controller.java中。
当我尝试下面的代码,我得到了HTTP状态500.
保护无效doGet(HttpServletRequest请求,HttpServletResponse响应)抛出ServletException,IOException {$ /
PrintWriter out = response.getWriter();
out.write(request.getPathInfo());
试试这个
$ b
request.getRequestDispatcher(/ view / electrics.jsp)。forward(req,res);
I have a small application (HTML form, servlet as controller and jsp files) and i try to figure out why i cannot to forward the request from servlet to jsp files.
the problem is after submit from html, showed up "HTTP Status 404"
Application flow:
- submit from html.
- controller get the name from html.
- controller supposed to move the request to jsp files.
thanks!
project hierarchy: 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>
update: Probably the problem is in the controller.java. When i try the following code, i got HTTP Status 500. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.write(request.getPathInfo());
}
Try this
request.getRequestDispatcher("/view/electrics.jsp").forward(req,res);
这篇关于从servlet转发请求到jsp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!