This discussion is archived
7 Replies Latest reply: Dec 26, 2012 7:56 AM by aksarben RSS

How to call a servlet from a browser

piratolo Newbie
Currently Being Moderated
Hello everybody,
I hope all of you understand my poor english; after solving many problems by reading this forum, i've just signed up, and this is my first question

I'd like to know how to call a servlet by using a URL.
Now i'm able to call the servlet this way http://localhost:8080/servlet/provaservlet/index.html beacuse i set my web.xml this way
<url-pattern>* * *.html</url-pattern>
Is there a way to call my servlet by using its name, for instance http://localhost:8080/servlet/servletFolder/ServletName?

I set the <url-pattern> element with everythings come to me, but i didn't get my goal yet!

Edited by: 976935 on 14-dic-2012 1.56
  • 1. Re: How to call a servlet from a browser
    gimbal2 Guru
    Currently Being Moderated
    Yes, you change the url-pattern to what you want it to be. Check out this free online book to learn how to properly build and deploy servlets:

    http://pdf.coreservlets.com
  • 2. Re: How to call a servlet from a browser
    piratolo Newbie
    Currently Being Moderated
    Hi, thank you for your help.
    Actually i've already read several articles about it; according to those articles/guides i have just to set the element <url-pattern> with the servlet name I set in the element <servlet-name>. But it's not working; probably i didn't understand well the directions beacuse of my poor english.
    Anyway i'll try to figure out a solution by reading the guide u posted.

    Edited by: 976935 on 14-dic-2012 3.14
  • 3. Re: How to call a servlet from a browser
    piratolo Newbie
    Currently Being Moderated
    I read it;      apparently everthing i did is correct.
    I set my web.xml file this way



    <?xml version="1.0" encoding="ISO-8859-1"?>
    <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
    <web-app>

    <servlet>
    <servlet-name>myServlet</servlet-name>
    <servlet-class>provaservlet.HelloWorldServlet</servlet-class>
    </servlet>

    <servlet-mapping>
    <servlet-name>myServlet</servlet-name>
    <url-pattern>/myServlet</url-pattern>
    </servlet-mapping>

    </web-app>

    Edited by: 976935 on 14-dic-2012 3.36
  • 4. Re: How to call a servlet from a browser
    gimbal2 Guru
    Currently Being Moderated
    976935 wrote:
    I read it;      apparently everthing i did is correct.
    I set my web.xml file this way



    <?xml version="1.0" encoding="ISO-8859-1"?>
    <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
    <web-app>

    <servlet>
    <servlet-name>myServlet</servlet-name>
    <servlet-class>provaservlet.HelloWorldServlet</servlet-class>
    </servlet>

    <servlet-mapping>
    <servlet-name>myServlet</servlet-name>
    <url-pattern>/myServlet</url-pattern>
    </servlet-mapping>

    </web-app>
    That looks good yes (but be careful with the servlet version, you likely want servlet specification 2.5 or 3.0). The servlet should then be accessible using http://localhost:8080/mywebapp/myServlet

    Here is a more up to date version of the web.xml:
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    
       <servlet>
         <servlet-name>myServlet</servlet-name>
         <servlet-class>provaservlet.HelloWorldServlet</servlet-class>
       </servlet>  
     
       <servlet-mapping>
         <servlet-name>myServlet</servlet-name>
         <url-pattern>/myServlet</url-pattern>
       </servlet-mapping>  
    
    </web-app>
    Notice the pretty formatting, I do that with \
     tags.
    
    Of course you need to be careful with other stuff you put in the web.xml, you must avoid url-patterns that overlap each other.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
  • 5. Re: How to call a servlet from a browser
    piratolo Newbie
    Currently Being Moderated
    Thank you, now it works.
    The correct url is http://localhost:8080/projectFolder/url-pattern and not http://localhost:8080/servlet/projectFolder/url-pattern

    Reading guides and articles i often saw the folder called "servlet", and i thought it was necessary to tell tomcat that i was calling a servlet.

    Actually, in the example i read on the internet, the folder called "servlet", was the name of the webapp :D

    Edited by: 976935 on 14-dic-2012 6.10

    Edited by: 976935 on 14-dic-2012 6.10
  • 6. Re: How to call a servlet from a browser
    gimbal2 Guru
    Currently Being Moderated
    976935 wrote:
    Thank you, now it works.
    The correct url is http://localhost:8080/projectFolder/url-pattern and not http://localhost:8080/servlet/projectFolder/url-pattern

    Reading guides and articles i often saw the folder called "servlet", and i thought it was necessary to tell tomcat that i was calling a servlet.

    Actually, in the example i read on the internet, the folder called "servlet", was the name of the webapp :D
    I can also be something very old. Tomcat had something called a "default servlet" (no longer activated by default and very much not recommended to be used) that would automatically map servlets to '/servlet/ServletName' without needing further configuration. That's the trouble of the internet - you have to find the truth among the lies and the outdated :)

    Be wary of that 'projectFolder' - its the webapps context name, not specifically some name in your project or application. It is likely that the context name is actually the same as the project name, but you are allowed to reconfigure it in several ways.

    Finally: this is not Tomcat specific. All this is part of the Java Enteprise Edition specifications. Tomcat just follows the servlets part of that standard. It might help if you don't search for information in the context of Tomcat specifically, but on the other hand the Tomcat website is a nice source of documentation when it comes to servlet & JSP things.
  • 7. Re: How to call a servlet from a browser
    aksarben Journeyer
    Currently Being Moderated
    Some additional info which may help your understanding:

    No browser ever "calls" a servlet (at least not in the sense that Java code "calls" a method). The browser simply sends a request string to a servlet container using the HTTP protocol (there are sockets, packets, etc. involved in that). The container (Tomcat in the case discussed here) examines the request, determines whether it is a GET or POST, then calls the doGet or doPost method of the appropriate servlet. This assumes, of course, that the container can decide which servlet is the right one (there can be, and often are, multiple servlets running in the container). That's where your web.xml comes in: If it has errors, the container will never figure out that your servlet is the one which should handle the request from the browser.

    A lot goes on between the time you press the Enter key & when your servlet gets to process your request. Understanding the request-reply cycle will go a long way in making you a better Web developer.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points