0

I'm new to Java Programming & having a tough time with Servlets & JSP, for the wide range of challenges it throws. For now, I'm unable to access the Servlet page due to this error:

 HTTP Status 404 - The requested resource is not available

This might seem to be a naive question for many, however after trying all the tips and tricks ranging from Stack Overflow to resorting to other study materials, I couldn't figure out the exact cause of the problem.

Servlet file:

package coreservlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/GoodCodeServlet")
public class GoodCodeServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

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

    response.setContentType("text/html");
    PrintWriter out = response.getWriter();

    String title = "Code Sample";
    String docType = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +      
                      "Transitional//EN\">\n"; 

    out.println(docType + "<html> \n" + 
                "<head> <title>" +title+ "</title></head>"  +
                "<body bgcolor=\"#eee\">" + 
                "<h1 align=\"center\">" +title+ "</h1>" +

                // Text inside a <pre> tag is displayed in a fixed-width font, 
                //and it preserves both spaces and line breaks....

                "<pre> \n" + getCode(request)+ "</pre>" +               
                "</body> </html>"  
             );

}

  protected String getCode(HttpServletRequest request) 
    {
       return (request.getParameter("code"));
    }

HTML file:

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body bgcolor="#FDEFD6">
    <center> <h1>Submit Code Samples</h1>

    <form action="/coreservlets.GoodCodeServlet" > 
    Code: <br><br> 
    <textarea rows="12" cols="40" name="code"></textarea> <br><br>
    <input type="submit" value="submit" /> 

    </form>
    </center>
    </body>
    </html>

web.xml file:

    <?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_3_0.xsd" version="3.0">

   <servlet>
   <servlet-name>CodeSample</servlet-name>
   <servlet-class>coreservlets.GoodCodeServlet</servlet-class>
   </servlet>

   <servlet-mapping>
   <servlet-name>CodeSample</servlet-name>
   <url-pattern>/coreservlets.GoodCodeServlet</url-pattern>
   </servlet-mapping>

   <servlet>
   <servlet-name>ShowParameters</servlet-name>
   <servlet-class>/coreservlets.ShowParameters</servlet-class>
   </servlet>

   <servlet-mapping>
   <servlet-name>ShowParameters</servlet-name>
   <url-pattern>/coreservlets.ShowParameters</url-pattern>
   </servlet-mapping>

   </web-app>
Amos M. Carpenter
  • 4,848
  • 4
  • 42
  • 72
mindfreak
  • 456
  • 3
  • 9
  • 29
  • You already defined annotation in servlet. Then there is no mapping required in web.xml.. Remove the mapping. And try with the mapping in Servlet Class.. – Shashi Mar 25 '15 at 08:18
  • 1
    change
    to
    – Shashi Mar 25 '15 at 08:19
  • Tried those tricks before posting, but somehow it didn't worked :( The funny part is other servlet programs which are part of the same package works absolutely fine having entries in the same web.xml file. – mindfreak Mar 25 '15 at 09:25
  • Could you please post the url once you submit the form.. – Shashi Mar 25 '15 at 09:30
  • http://localhost:8080/coreservlets.GoodCodeServlet?code=printf%28%22Example+of+bad+code%22%29%3B – mindfreak Mar 25 '15 at 09:35
  • check the context path.. – Shashi Mar 25 '15 at 11:18
  • @Shashi - If I change the context path to http://localhost:8080/PDF_Servlet/GoodCodeServlet?code=printf%28%22Example+of+bad+code%22%29%3B it displays the desired results... However, is there a way out to fix this permanently ? – mindfreak Mar 25 '15 at 14:30
  • http://stackoverflow.com/questions/8448057/use-relative-path-for-form-actions-in-jsp http://stackoverflow.com/questions/16683877/form-action-sampleservlet-giving-me-exception – Shashi Mar 26 '15 at 07:19

1 Answers1

0

Context path is missing from form action.

Please refer :

Use Relative Path for Form Actions in jsp

Form Action sampleservlet giving me exception

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Shashi
  • 12,487
  • 17
  • 65
  • 111