Request forward from servlet to jsp

I have a small application (HTML form, a servlet as a controller and jsp files) and I'm trying to understand why I cannot redirect a request from a servlet to jsp files.

problem after sending from html, "HTTP Status 404" appeared

Application Stream:

  • send from html.
  • the controller gets the name from html. Controller
  • should 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: There is probably a problem in the .java controller. When I try to execute the following code, I got an HTTP status of 500. protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

  PrintWriter out = response.getWriter(); out.write(request.getPathInfo()); } 
+7
java html jsp model-view-controller servlets
source share
3 answers

try it

  request.getRequestDispatcher("/view/electrics.jsp").forward(req,res); 
+7
source share

web.xml

 <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> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> 
0
source share

Your code works fine in my local file with web.xml defined at the end with or without a welcome list. Try using your code with dynamic web content version 2.5, where web.xml is created automatically.

 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <description> CouponsServer </description> <display-name>Controller for CouponsServer</display-name> <servlet> <display-name>Controller</display-name> <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> 
0
source share

All Articles