Reputation: 1
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.
Upvotes: 0
Views: 281
Reputation: 10184
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:
web.xml
to map the url pattern /listEmployees.jsp
to a servlet with a servlet class of getEmployees
.doGet
method in your getEmployees
servlet and call the processRequest
method from it.Upvotes: 0
Reputation: 3967
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);
}
Upvotes: 1
Reputation: 620
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>
Upvotes: 1
Reputation: 953
processRequest
is not answering any requests himself, it is just support method, it will be executed upon request if and only if it was called from doGet
or doPost
, which are actually responding to requests, if you want to both methods execute same code, just call that processRequest
from doPost
and doGet
, more here
Upvotes: 0