I am working on a project for school and am completely frustrated. The project is to get a simple JSP/Servlet demo working on our personal websites and after probably 20+ hours of work I am still unsuccessful in getting this accomplished. The really frustrating thing I’ve been given all the code and still can’t really get this to work. Well, that isn’t totally true. I have gotten it to work on my local tomcat server, but I can’t replicate that magic on my personal website. So, here is what I have.
First, the code. This assignment consists of two classes for servlets and two jsps and a web.xml file.
Here is the first servlet called ControllerServlet.java
package test;
// Import the servlet classes
import javax.servlet.*;
import javax.servlet.http.*;
// Import the standard Java classes
import java.io.*;
import java.util.*;
/**
* Controller Servlet
*/
public class ControllerServlet extends HttpServlet
{
private static String HELLO_JSP = "hello.jsp";
private static String GOODBYE_JSP = "goodbye.jsp";
private static String OTHER_JSP = "main.jsp";
public void init() throws ServletException
{
// Typically initialize your request to page mappings here
}
public void service( HttpServletRequest req, HttpServletResponse res ) throws ServletException
{
try
{
// Get the path - this is our key to our Request map
String pathInfo = req.getPathInfo();
// Find out what the user is requesting
String jsp = null;
if( pathInfo.equalsIgnoreCase( "/hello" ) )
{
String name = req.getParameter( "name" );
PersonBean person = new PersonBean( name );
HttpSession session = req.getSession();
session.setAttribute( "person", person );
jsp = HELLO_JSP;
}
else if( pathInfo.equalsIgnoreCase( "/goodbye" ) )
{
HttpSession session = req.getSession();
PersonBean person = ( PersonBean )session.getAttribute( "person" );
req.setAttribute( "person", person );
session.removeAttribute( "person" );
jsp = GOODBYE_JSP;
}
else
{
jsp = OTHER_JSP;
}
// Foward the request to the jsp
RequestDispatcher dispatcher = req.getRequestDispatcher( "/" + jsp );
dispatcher.forward( req, res );
}
catch( IOException ioe )
{
throw new ServletException( ioe );
}
}
}
Now the PersonBean.java:
package test;
public class PersonBean implements java.io.Serializable
{
private String name;
public PersonBean()
{
}
public PersonBean( String name )
{
this.name = name;
}
public String getName()
{
return this.name;
}
public void setName( String name )
{
this.name = name;
}
}
Now the hello.jsp:
<jsp:useBean id="person" class="test.PersonBean" scope="session" />
<HTML>
<HEAD>
<TITLE>Hello, <jsp:getProperty name="person" property="name" /></TITLE>
</HEAD>
<BODY>
<P>Hello, <jsp:getProperty name="person" property="name" /></P>
<A HREF="goodbye">Goodbye</A>
</BODY>
</HTML>
And the goodbye.jsp
<jsp:useBean id="person" class="test.PersonBean" scope="session" />
<HTML>
<HEAD>
<TITLE>Good bye, <jsp:getProperty name="person" property="name" /></TITLE>
</HEAD>
<BODY>
<P>Good bye, <jsp:getProperty name="person" property="name" /></P>
</BODY>
</HTML>
Lastly my web xml file:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>deploy</display-name>
<servlet>
<description>
</description>
<display-name>ControllerServlet</display-name>
<servlet-name>ControllerServlet</servlet-name>
<servlet-class>test.ControllerServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ControllerServlet</servlet-name>
<url-pattern>/testing/*</url-pattern>
</servlet-mapping>
</web-app>
My web server’s file manager has a folder called WEB-INF under the public_html section so in that file folder I placed the two jsps and the web xml file. Under that was a preexisting folder called classes which I made a new folder in called test and in that folder placed the two class files. I would think that going to mysite.com/testing/hello?name=Bradley would result in the correct thing happening, but unfortunately I get a 404 error saying that url testing/hello is not found.
Does anyone have any suggestions as to why and can’t make this happen?
The servlet class declaration in
web.xmlis wrong. You’re putControllerServletin a packagetest. So the proper servlet class declaration should be:How you got it to work at local environment is beyond me. Perhaps the package was initially not there at all, or perhaps you removed the package from the servlet class declaration, or perhaps you’ve 2 servlet classes in both the default and
testpackage.Update: based on the code given as far (and assuming that you fixed the servlet declaration in
web.xml), here’s how the folder structure should look like: