1

I am new to servlets .I am using eclipse juno for this.I am having a trouble in running my program..My code is

package sTraining;

import java.io.*;
import javax.servlet.*;

public class Servlet1 implements Servlet{
ServletConfig config=null;

public void init(ServletConfig config){
this.config=config;
System.out.println("servlet is initialized");
}

public void service(ServletRequest req,ServletResponse res)
throws IOException,ServletException{

res.setContentType("text/html");

PrintWriter out=res.getWriter();
out.print("<html><body>");
out.print("<b>hello simple servlet</b>");
out.print("</body></html>");

}
public void destroy(){System.out.println("servlet is destroyed");}
public ServletConfig getServletConfig(){return config;}
public String getServletInfo(){return "copyright 2007-1010";}

} 

I am getting this error[http://localhost:8080/Test/WEB-INF/classes/sTraining/Servlet1.java][1] although i have this thing in my web .xml file

<servlet>
    <description></description>
    <display-name>Servlet1</display-name>
    <servlet-name>Servlet1</servlet-name>
    <servlet-class>servlet.Servlet1</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Servlet1</servlet-name>
    <url-pattern>/Servlet1</url-pattern>
  </servlet-mapping>

why this is not running? My code is fine. First time when I run this page it run, but running this program after my second program it did not run and that second program also not run.

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
user2502227
  • 495
  • 1
  • 10
  • 23

2 Answers2

4

Why are you accessing

http://localhost:8080/Test/WEB-INF/classes/sTraining/Servlet1.java 

? You should be accessing

http://localhost:8080/Test/Servlet1

Read the above as

[protocol or scheme] :// [host] : [port] / [context] / [servlet mapping]

Also, according to the source code you've posted. The Servlet1 class is in package sTraining. Your web.xml should therefore have

<servlet>
    <description></description>
    <display-name>Servlet1</display-name>
    <servlet-name>Servlet1</servlet-name>
    <servlet-class>sTraining.Servlet1</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>Servlet1</servlet-name>
    <url-pattern>/Servlet1</url-pattern>
</servlet-mapping>

A Servlet Container will not make anything in the WEB-INF folder available to client requests.


What you are doing is not great practice. Your class should probably extend HttpServlet to get some standard HTTP behavior. You also shouldn't be writing HTML in Java code. Try reading the tutorial and references we have on Stackoverflow, here.

Community
  • 1
  • 1
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • You could also add that every file inside WEB-INF folder cannot be accessed through a client – Luiggi Mendoza Sep 16 '13 at 21:00
  • *your class should extend `HttpServlet` to get some standard behavior* this applies for beginners on the matter, but when creating a framework like Spring MVC or JSF, it is better to implement the `Servlet` interface than extending from `HttpServlet`. – Luiggi Mendoza Sep 16 '13 at 21:02
  • @LuiggiMendoza I don't know for JSF, but Spring's `DispatcherServlet` extends `HttpServlet` somewhere in its inheritance tree. What would you recommend I put instead? Go ahead and edit the answer. I'll wiki it. – Sotirios Delimanolis Sep 16 '13 at 21:03
  • As said, if you want to create a MVC framework, your servlet should implement the `Servlet` interface instead of just extending `HttpServlet` in order to manage everything by yourself. This is better explained here: http://stackoverflow.com/q/11530152/1065197 – Luiggi Mendoza Sep 16 '13 at 21:06
  • this also give me same result – user2502227 Sep 16 '13 at 21:09
  • 1
    @user2502227 Your class' package is `sTraining`. The `servlet-class` should be `sTraining.Servlet1` instead of `servlet.Servlet1`. – Sotirios Delimanolis Sep 16 '13 at 21:09
1

Put ./Servlet1 in your form action attribute

<form action="./Servlet1">
....
</form>

and check your web.xml your package name is different

    <servlet>
    <description></description>
    <display-name>Servlet1</display-name>
    <servlet-name>Servlet1</servlet-name>
    <servlet-class>sTraining.Servlet1</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Servlet1</servlet-name>
    <url-pattern>/Servlet1</url-pattern>
  </servlet-mapping>
KhAn SaAb
  • 5,248
  • 5
  • 31
  • 52
  • *Put ./Servlet1 in your form action attribute* OP is accessing the servlet through a GET request, this will work if OP wants to submit data from JSP to this servlet. – Luiggi Mendoza Sep 16 '13 at 21:11
  • yes i know,i have mention the way. if OP wants to request through JSP or HTML – KhAn SaAb Sep 16 '13 at 21:15