0

I am trying to revive an old project, I already converted it to Dynamic web module and made the jsp files working, however, the controllers cannot be seen even though I already mapped it in web.xml. It says: The requested resource (/testproject/servlet/testproject.Controller.TestController) is not available.

Note: I am running this using tomcat 4 and java 1.5. Btw, I replaced some names to 'test...' to censor the name. Just in case.

Project Structure

Web.xml (I edited the web.xml highlighted in the Project Structure image:

    <?xml version="1.0" encoding="UTF-8"?>
<web-app 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">
  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>TestController</servlet-name>
    <servlet-class>testproject.Controller.TestController</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>TestController</servlet-name>
    <url-pattern>/Test_Project/testproject/servlet/TestController</url-pattern>
  </servlet-mapping>

</web-app>

TestController.java (this is under WebContent>testproject>WEB-INF>classes>Controller):

    public class TestController extends HttpServlet {
    
    private static final long serialVersionUID = -2222971807425568374L;

    /**
     * Constructor of the object.
     */
    public TestController() {
        super();
    }

    /**
     * Destruction of the servlet. <br>
     */
    public void destroy() {
        super.destroy(); // Just puts "destroy" string in log
        // Put your code here
    }

    /**
     * The doGet method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to get.
     * 
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
            processRequest(request, response);
    }

    /**
     * The doPost method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to post.
     * 
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doPost(
        HttpServletRequest request,
        HttpServletResponse response)
        throws ServletException, IOException {
            processRequest(request, response);
    }

    /**
     * Initilaisation of the servlet. <br>
     *
     * @throws ServletException if an error occure
     */
    public void init() throws ServletException {
        // Put your code here
    }

    public void processRequest(
        HttpServletRequest request,
        HttpServletResponse response)
        throws ServletException, IOException {

        //censored codes here
        
        RequestDispatcher rd = getServletContext().getRequestDispatcher(url);
        rd.forward(request, response);

        return;
    }
}

JSP page (under WebContent>testcontroller>jsp)

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String baseURL="Test_Controller/testproject/servlet/testproject.";
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+"/";
%>

<meta HTTP-EQUIV="REFRESH" content="0; url=<%=basePath%><%=baseURL%>Controller.TestController?userid=">
  • did you try starting with '/' in URL pattern node /Test_Project/testproject/servlet/TestController – office.aizaz Apr 11 '22 at 08:15
  • yes, tried it as well.. – Iskooolar2 iskoolar2 Apr 11 '22 at 08:21
  • The path given in your jsp page, i don't see the context-root defined anywhere. What is the name of your app? Are you creating a '.war'? Normally I would expect the servlet resources be available at e.g. http://localhost:8080/myapp/Test_Project/testproject/servlet/TestController In your case, what is defined as context root? Accordingly you have to adjust your url patterns – office.aizaz Apr 12 '22 at 07:55
  • hi @office.aizaz, the name of my app is Test_Project... What do you mean by context root though? Sorry it's my first time reviving an old project that's not maven :( – Iskooolar2 iskoolar2 Apr 12 '22 at 08:08

0 Answers0