Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Jsp How to work with POST method in jsp page

Related Tutorials


 
 

Share on Google+Share on Google+

How to work with POST method in jsp page

Advertisement
This is detailed JSP code, how to use POST method instead of GET method in jsp page. GET is default method for sending the request to the server.

How to work with POST method in jsp page

     

This is detailed JSP code, how to use POST method instead of GET method in jsp page. GET is default method for sending the request to the server. The difference between these two methods has been described below:

 

 

 

 

Difference between GET and POST

GET:

1. URL Changes to the submitted script name, appended with a list of each variable with the value.
2. Use only if the number of variable to be used in a form are very less.
3. Never use GET forms when asking for login ID and passwords.
4. Even hidden variables are shown as a part of the URL.
5. Web servers might complain about long URLs being submitted. A lot of times a URL 255 char or more is a problem.
POST:

1. This is the best way of submitting forms to the web server.
2. There is no limitation on the number of Variables passed from the form.
3. This is a transparent way of transmitting variables to the web server where hidden variable are always hidden!

jsp_with_post_method.jsp: Using POST method is nothing different than using GET method as request method in JSP. For this we just need to set the value 'POST' for the 'method' attribute of html 'form' element.

<HTML>
<HEAD>
    <TITLE>first page...create session</TITLE>
</HEAD>
<BODY bgcolor="#6E6E6E">
    <% String name = "ravi";
            session.setAttribute("user", name);
    %>
        // use POST  method to avoid show data in url 
    <FORM NAME="form1" ACTION="jsp_with_post_method.jsp" METHOD="POST">
        <TABLE bgcolor="#D8D8D8">
        <tr>
            <td> Enter name </td>
            <td><input type="text" name="txtName"></td>
        </tr>
        <tr>
            <td> Enter Email id </td>
            <td><input type="text" name="email"></td>
        </tr>
        <tr align="center"><td></td>
        <td><INPUT TYPE="submit" VALUE="show"></td>
    </FORM>
    
        <%
            if (request.getParameter("txtName") != null && 
			   request.getParameter("txtName") != null) {
                if (!(request.getParameter("txtName").equals("")) &&
			!(request.getParameter("txtName").equals(""))) {%>
    <tr>
        <td> Your name is </td>
        <td><%=request.getParameter("txtName")%></td>
    </tr>
    <tr>
        <td> Email id is </td>
        <td><%=request.getParameter("email")%></td>
    </tr>
    <%}
            }
    %>
    </TABLE>
</body> 
</html>

Save this code with extension .jsp in your application directory ("user" directory, in our case) in tomcat server. To run this application first start Tomcat server by click on startup.bat file in  tomcat-6.0.16/bin then open browser and type the url http://localhost:8080/user/jsp_with_post_method.jsp in address bar.

Enter name and email id in the appropriate text box and click on show button.

Download source code

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: July 7, 2008

Related Tutorials

Discuss: How to work with POST method in jsp page  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com