user680343
user680343

Reputation: 55

How to use tomcat7 to run servlets

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:

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?

Upvotes: 1

Views: 7382

Answers (2)

olupot douglas
olupot douglas

Reputation: 11

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.

Upvotes: 1

Bozho
Bozho

Reputation: 597016

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.

Upvotes: 2

Related Questions