1
votes

I am a newbie in servlet. I am using tomcat7 and I want to run a servlet. I have modified web.xml and put in the WEB-INF dir. Contents are

<servlet>
    <servlet-name>asg1</servlet-name>
    <servlet-class>asg1</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>asg1</servlet-name>
    <url-pattern>asg1</url-pattern>
</servlet-mapping>

My appname is servlet. I haven't used any .war file. I have created "servlet" dir. SO my servlet DIR is:

  • /servlet/WEB-INF/classes
  • /servlet/WEB-INF/web.xml
  • /servlet/asg1.html

I have put this servlet DIR into {tomcat-asInstall}/webapps I am accessing it with url http://localhost:8080/servlet

but It can not be accessed. Other apps which are provided by tomcat run very well but why not my servlet?

2

2 Answers

2
votes

You need to access http://localhost:8080/servlet/asg1. But first change the pattern to be /asg1. In fact in tomcat 7 (and servlets 3) you can skip the XML and use @WebServlet to map the servlet.

1
votes

You might want to change the URL pattern to something like this.

  1. compile the servlet and copy its class file (the .class)

  2. I'd advise ypu create a folder called "classes" inside "webapps" in the root folder for your class files

  3. paste the class file in that folder and open the XML descriptor file then do the following:

    <servlet>
      <servlet-name>asg1</servlet-name>
      <servlet-class>.class file name</servlet-class>
    </servlet>
    <servlet-mapping>
      <servlet-name>asg1</servlet-name>
      <url-pattern>/classes/asg1</url-pattern>
    </servlet-mapping>
    

Hope it helps.