简体   繁体   中英

Why won't my servlet forward to my .jsp?

Here is my code:

Index.html

<html>
    <head>
        <title>Web App Assignment 5</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <div>
            <a href="listEmployees.jsp">View Employees</a>
        </div>
    </body>
</html>

Employees.java

class Employees {

        private String name;
        private int id;

        public Employees() {

        }
        public Employees(String name, int id){
            setName(name);
            setID(id);
        }

        public String getName(){
            return name;
        }

        public int getID(){
            return id;
        }

        public void setName(String name){
            this.name = name;
        }

        public void setID(int id) {
            this.id = id;
        }
    }

getEmployees.java

import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.ArrayList;
    import java.util.List;
    import javax.servlet.RequestDispatcher;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;

    public class getEmployees extends HttpServlet {

        protected void processRequest(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            response.setContentType("text/html;charset=UTF-8");

            try (PrintWriter out = response.getWriter()) {

            Employees emp1 = new Employees("Jim", 002);
            Employees emp2 = new Employees("Pam", 043);
            Employees emp3 = new Employees("Dwight", 013);
            Employees emp4 = new Employees("Kevin", 123);
            Employees emp5 = new Employees("Michael", 001);
            Employees emp6 = new Employees("Oscar", 033);

            List<Employees> employees = new ArrayList<Employees>();
            employees.add(emp1);
            employees.add(emp2);
            employees.add(emp3);
            employees.add(emp4);
            employees.add(emp5);
            employees.add(emp6);

            HttpSession session = request.getSession();
            session.setAttribute("empl", employees);

            RequestDispatcher dispatcher = request.getRequestDispatcher("listEmployees.jsp");
            dispatcher.forward(request, response);
                }
            }
    }

listEmployees.jsp

    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>List of Employees </title>
    </head>
    <body>
        <h1><u> List of Employees </u> </h1>
        <table>
            <thead>
                <tr>

                <td>Employee Name</td> 
                <td></td>
                <td>Employee ID</td>
        </tr>
    </thead>
    <c:forEach items="${empl}" var="current">
        <tr>
          <td><c:out value="${empl}" /><td>
        </tr>
      </c:forEach>
</table>
</body>
</html>

I'm trying to get the index page to forward to the getEmployees servlet which is then supposed to forward the list of employees to the JSP and display the list of employees. I am unsure of why the servlet is not forwarding to the jsp. Any help would be much appreciated. Using Glassfish server.

You must change the variable that you are printing in the foreach:

<c:forEach items="${empl}" var="current">
    <tr>
        <td><c:out value="${current.name}" /><td>
        <td><td>
        <td><c:out value="${current.id}" /><td>
    </tr>
</c:forEach>

The HttpServlet has two important methods for handling the client's request:

1. doPost: in general handles requests coming from forms with HTTP POST method

2. doGet: handled requests coming from get method.

Now, your processRequest method, is just your code which is not bound ( overridden ) to anything.

You can easily call it from the above methods in order to not complicate the code in them thus the requests are handled in it.

So in order to use processRequest to handle your request you have to actually call it from one of those two methods.

Just add to your Servlet those methods that are going to take the requests and regardless the request type they will execute processRequest

protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
  processRequest(request, response);
}

and

protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
  processRequest(request, response);
}

processRequest本身并没有回答任何请求,它只是一种支持方法,仅当它是从实际上响应请求的doGetdoPost调用时才在请求时执行的,如果您想两个方法都执行相同的代码,从doPostdoGet调用该processRequest在这里更多

You have not shown the content of your web.xml. But from the description of the problem, it sounds like your container is not able to find any servlet which is mapped to a url pattern like /listEmployees.jsp . No, getEmployees is not the servlet which will be called because it doesn't override the doGet or doPost method. You have one method called processRequest . But the container doesn't know what that method is. It only knows doPost or doGet or a service method none of which is overridden by your getEmployees servlet. It looks like (without seeing your web.xml ), the container is simply dispatching the request to the JSP because of the settings in your web.xml. Do the following things to fix your problem:

  1. Correct the settings in your web.xml to map the url pattern /listEmployees.jsp to a servlet with a servlet class of getEmployees .
  2. Override doGet method in your getEmployees servlet and call the processRequest method from it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM