Results 1 to 5 of 5

Thread: Servlet Help

  1. #1
    HeavyTech is offline Member
    Join Date
    Jun 2014
    Posts
    30
    Rep Power
    0

    Default Servlet Help

    i just started a new course in web programming and my assignment is to create two servlets.

    One of them has messages post by the user and the other servlet allows you to write a post.
    I am having trouble being able to write a post and connect it to the other servlet


    Also i created a class that contains Title and message. It has its proper getters and setter.
    now, i am having problem where you click submit to it gathers that info and properly puts into the Message post servlet

    This is my my comment servlet. i want to be able to transfer this info into my other servlet.
    Java Code:
    out.println("<form action=\".....Does Servlet 1 go here?" method=\"post\"><div class=\"row\">
    out.println("<input name=\"title\" type=\"text\" class=\"form-control\" placeholder=\"Post Title\">");
    out.println("<p><p class=\"lead\"><textarea name=\"comment\" placeholder=\"comment\" class=\"form-control\" rows=\"3\"></textarea>");

    Any suggestions?

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Servlet Help

    The HTML produced should have a form that posts to the servlet that processes the message.
    So yes, the form action should go to your processing servlet.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Servlet Help

    In the case of servlets a very specific example can help. A servlet requires two things to work: the code and a configuration. The configuration part is to tell under which HTTP request path the server should be invoking the servlet. There are multiple ways to do the configuration (basically an easy code way and a boring XML way), I'm assuming this course will show you the old boring way though the web.xml. The configuration of a servlet might look like this:

    Java Code:
        <!-- 1: give the servlets an identifiable name -->
        <servlet>
            <servlet-name>MessageSubmitServlet</servlet-name>
            <servlet-class>com.somepackage.MessageSubmitServlet</servlet-class>
        </servlet>
    
        <servlet>
            <servlet-name>MessageDisplayServlet</servlet-name>
            <servlet-class>com.somepackage.MessageDisplayServlet</servlet-class>
        </servlet>
    
        <!-- 2: map an url to the servlets -->
        <servlet-mapping>
            <servlet-name>MessageSubmitServlet</servlet-name> <!-- name must be the same as in the '1:' section right above here -->
            <url-pattern>/MessageSubmitServlet</url-pattern>
        </servlet-mapping>
    
        <servlet-mapping>
            <servlet-name>MessageDisplayServlet</servlet-name>
            <url-pattern>/MessageDisplayServlet</url-pattern>
        </servlet-mapping>
    With this configuration the server knows that the servlet exists in your application and which specific url will cause it to be 'invoked'. This url is always relative to the base url of your web application. So if the application can be reached through the url http://localhost:8080/myapp, then the above servlet's full urls are http://localhost:8080/myapp/MessageSubmitServlet and http://localhost:8080/myapp/MessageDisplayServlet.

    Now comes the tricky part where people often run into confusion: actually referring to such a web resource. In this case we want to submit to MessageDisplayServlet from the MessageSubmitServlet. Thus the html form will need to look like this:

    Java Code:
    <form method="POST" action="MessageDisplayServlet">
    ...
    </form>
    Now note how that action does not begin with a '/'; I'm using a so called 'relative' path. What will happen with this relative url is that the browser takes the path of the current resource and then appends what you have in your action. Thus the resource 'http://localhost:8080/myapp/MessageSubmitServlet' will become 'http://localhost:8080/myapp/MessageDisplayServlet' because it appends the action to the path 'http://localhost:8080/myapp/'.


    If instead I would make the common mistake of pasting into the action literally what is in the url-pattern of the web.xml:

    Java Code:
    <!-- WARNING: THIS IS AN EXAMPLE OF DOING IT WRONG! -->
    <form method="POST" action="/MessageDisplayServlet">
    ...
    </form>
    The url I'm using here is 'absolute'. What the browser will do in this case is use the literal url that you provide; the full url will become 'http://localhost:8080/MessageDisplayServlet' which is wrong; the 'myapp' part is now missing in between. This would have worked:

    Java Code:
    <!-- WARNING: THIS IS AN EXAMPLE OF DOING IT WRONG! -->
    <form method="POST" action="/myapp/MessageDisplayServlet">
    ...
    </form>
    Because then I force the absolute url to be correct. The problem is that as soon as the name of your web application ('myapp') changes, the above url won't work anymore. It is not a good idea to hardcode such details.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    HeavyTech is offline Member
    Join Date
    Jun 2014
    Posts
    30
    Rep Power
    0

    Default Re: Servlet Help

    Thank you for the replies.
    I got my project running :)
    I could sleep now haha

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Servlet Help

    Just for completeness, the web.xml servlet configuration is very much outdated. If you run on a modern server (example: Tomcat 7 or higher) you can simply do this in your code:

    Java Code:
    @WebServlet("/MessageDisplayServlet") // this annotation replaces the web.xml configuration
    public class MessageDisplayServlet extends HttpServlet {
      ...
    }
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Replies: 1
    Last Post: 11-14-2013, 10:29 AM
  2. Replies: 13
    Last Post: 07-25-2013, 05:30 PM
  3. Replies: 1
    Last Post: 08-23-2011, 11:12 AM
  4. Replies: 1
    Last Post: 12-02-2008, 12:23 AM
  5. Replies: 1
    Last Post: 08-07-2007, 03:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •