0

I have created a sample project to test jsp. Project looks like this

I am using tomcat 9, Java SE 8_73. web module version for the project is 3.1 without generated web.xml. The

The java code looks like this

package pac;

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;

/**
 * Servlet implementation class JSPProject
 */
@WebServlet("/jspproject")
public class JSPProject extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter cut = response.getWriter();
        cut.println("hello world");
    }
}

but when run the JSPProject.java on the server, below error message is displayed.

HTTP Status 404 - /DynamicJSP/jspproject
type Status report`
message /DynamicJSP/jspproject
description The requested resource is not available.
The requested resource is not available.
s1n7ax
  • 2,750
  • 6
  • 24
  • 53
  • Instead of `service` method use `protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {}` – Naman Apr 14 '16 at 06:21
  • not working. im gatting the same error – s1n7ax Apr 14 '16 at 06:25
  • Try cleaning project & restart tomcat. – Naman Apr 14 '16 at 06:53
  • JSP are text files, not writing to a response. There already is a Jsp servlet in Tomcat. Just name a text file abc.jsp and put it in webcontent (the directory above WEB-INF). 404 means the url you passed, could not be served. So either your app is not called "DynamicJSP", or your serlvet did not get initialized. Check component scanning, or add a web.xml and set metadata-complete=false. – Stefan Apr 14 '16 at 07:39
  • Your question has nothing to do with JSP. It's just a plain webservlet. – Martijn Burger Apr 14 '16 at 08:31
  • Give me the complete URL you are trying to open – ACV Apr 14 '16 at 08:46
  • http://localhost:8080/DynamicJSP/jspproject – s1n7ax Apr 14 '16 at 08:57

2 Answers2

0
    if you have changed your method to doGet and after that also it doesn't works then you need to make changes in your web.xml file. Pls look at below example :

<servlet>
<servlet-name>HelloServlet</servlet-name> (this is any name of your choice)
<servlet-class>examples.Hello</servlet-class>(This is Java class implements the servlet)
</servlet>
<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>(this name should exactly the same name as above , used for mapping)
<url-pattern>/hello</url-pattern> (This is URL pattern that invokes the servlet in your browser)
</servlet-mapping>
arjun99
  • 358
  • 1
  • 8
-1

You should override the doGet method from the HttpServlet class you are extending like this:

  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp)
          throws ServletException, IOException {
    try (ServletOutputStream cut = resp.getOutputStream()) {
      cut.println("hello world");
      cut.flush();
    }
Martijn Burger
  • 7,315
  • 8
  • 54
  • 94