Solved

POST AND GET IN Servlet

Posted on 2007-04-07
19
1,672 Views
Last Modified: 2013-11-24
Hi all,

I just want to clear some of the douts here.

Can I include html file with METHOD=POST in servlet. I've tried couple of times and it's not working. some of them saying , POST METHOD will not work when u embedded html in servlet file. If u want to use POST METHOD u need to have a seperate html file.

Is that true?.....

If that true, In that case, when we print the records from database how do I display the records using post method?...because it's very difficult for me to display the records in a sepearte html file.

thanks
0
Comment
Question by:sivakugan
  • 10
  • 7
  • 2
19 Comments
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 18870276
> Is that true?.....
 
No. You need to implement a servlet that would handle the payload, by default its not done.
0
 

Author Comment

by:sivakugan
ID: 18870283
What does it mean by payload?..Can u explain it for me?.plzzzzzzzzz
0
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 18870335
payload is the body of the HTTP protocol, in this case, the file you want to send.
0
 

Author Comment

by:sivakugan
ID: 18870372
if u send me a link, which has sample coding it would be grateful for me.
thanks
0
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 18871784
0
 

Author Comment

by:sivakugan
ID: 18872067
Hi Ajay,
I've seen those links. It doesn't solve my problem indeed and i haven't got any idea about java beans.
The tutorial is explaining about beans stuff. The reason for my question is- I'm  trying to use post method with servlet but it's not working for me. It says "HTTP Status 405 - HTTP method GET is not supported by this URL". but in this case i'm not using GET anywhere else in my code. It's confusing for me.
but if i use post method seperately in a html file, it's working for me. but in this case i can't use html file as a seperate one. because i'm displaying some records from db and making user to press the  button to do more thing.

I hope u understand my problem. further more..I put my code here...

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;

public class potty extends HttpServlet
{


      Connection connection;
      

      public void init()
      {

            try
            {
                  Class.forName("org.gjt.mm.mysql.Driver");            
                  //loading a driver

                    connection = DriverManager.getConnection("jdbc:mysql://url","username","password");
                  //connecting to a database

            }

            catch (Exception a)

            {

                              

                  a.printStackTrace();



            }


      }



      public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException
      {


            response.setContentType("text/html");
            PrintWriter out=response.getWriter();
            out.println("<HTML>");
            out.println("<BODY>");
            

            try
            {
                  Statement statement = connection.createStatement();
                  ResultSet rs=statement.executeQuery("select * from races;");
                  while (rs.next())
                        {
                              out.println(rs.getString("thalai"));
                              out.println("<br>");
                              out.println(rs.getString("vilakam"));
                              out.println("<br>");
                              out.println("");
                              out.println(rs.getString("S1")+""+rs.getString("ottay1"));
                              out.println("");                              
                              out.println("<FORM METHOD=\"POST\" ACTION=\"http://\">");
                              out.print("<INPUT TYPE=\"SUBMIT\" VALUE=\"kattu!\">");
                              out.println("</FORM>");


                        }

                  

            }


            catch (Exception e)

            {
                  out.println(e);



            }

            out.println("</BODY>");
            out.println("</HTML>");










      }







}

0
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 18872189
Add this to this class

public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException {
   doPost(request, response);
}
0
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 18872190
You are getting this error because the servlet doesn't implement doGet method (handler of the GET method)
0
 

Author Comment

by:sivakugan
ID: 18872271
Does it necessary to implement doGet method (handler of the GET method) in servlet?..When I don't want to use this doGet method and trying to use only doPost. because one of the requirements specification is only use post method. thats why i'm asking.
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 18872277
> Does it necessary to implement doGet method (handler of the GET method) in servlet?..

No. If you don't implement, and access through GET method - you will get an error like you are getting now.
0
 

Author Comment

by:sivakugan
ID: 18872285
Hi ajay,

When I use that. will it show the username and password as a querystring in url?
0
 

Author Comment

by:sivakugan
ID: 18872302
Hi ajay, even though I used that method (the one u mentioned), it doesn't resolve my problem. but this time it's giving me the error in other way. which is "HTTP Status 405 - HTTP method POST is not supported by this URL"
0
 

Author Comment

by:sivakugan
ID: 18873156
Hi, who can challenge this to answer the question?..
0
 
LVL 23

Accepted Solution

by:
Ajay-Singh earned 300 total points
ID: 18874062
quite wired... show me full code including web.xml
0
 

Author Comment

by:sivakugan
ID: 18874993
I'm using external server to compile and run it from their's url on the browser. so i can't get the web.xml. i'm afraid.
0
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 18879238
How you are accesing your servlet? by typing the URL on browser?
because that will invoke doGet bydefault as Ajay has mentioned.. if you have added the doGet method.. it should be working.
0
 

Author Comment

by:sivakugan
ID: 18881131
yes. I'm accessing the servlet by typing the URL on the browser.but at the same time i don't want to use get method. also as u said get is working fine. I want to use post method due to the security reasons .
0
 
LVL 19

Assisted Solution

by:Kuldeepchaturvedi
Kuldeepchaturvedi earned 200 total points
ID: 18881318
when you are accesing the servlet for the first time ( via typing url..) IT WILL call the get method..
now once you have the HTML form, after that you can call the post method..

 DoGet becomes the security threat only if you are passing some parameters to your servlet because they are visible on the URL.. when you are accessing the servlet for first time.. there is no parameters that are getting passed. hence you should not be having any problems with the security.
0
 

Author Comment

by:sivakugan
ID: 18935495
As far as concerned, using HTML GET did not allow us to send both handwired parameters and parameters from a form at the same time. This is a limitation of HTTP and not Servlets.
So In my case, I need to use POST. Further more, POST won't work if u included in servlet. The HTML POST should be in a seperate HTML file. thats wat i come up finally.

thanks
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now