POST AND GET IN Servlet

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
sivakuganAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ajay-SinghCommented:
> Is that true?.....
 
No. You need to implement a servlet that would handle the payload, by default its not done.
0
sivakuganAuthor Commented:
What does it mean by payload?..Can u explain it for me?.plzzzzzzzzz
0
Ajay-SinghCommented:
payload is the body of the HTTP protocol, in this case, the file you want to send.
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

sivakuganAuthor Commented:
if u send me a link, which has sample coding it would be grateful for me.
thanks
0
sivakuganAuthor Commented:
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
Ajay-SinghCommented:
Add this to this class

public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException {
   doPost(request, response);
}
0
Ajay-SinghCommented:
You are getting this error because the servlet doesn't implement doGet method (handler of the GET method)
0
sivakuganAuthor Commented:
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
Ajay-SinghCommented:
> 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
sivakuganAuthor Commented:
Hi ajay,

When I use that. will it show the username and password as a querystring in url?
0
sivakuganAuthor Commented:
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
sivakuganAuthor Commented:
Hi, who can challenge this to answer the question?..
0
Ajay-SinghCommented:
quite wired... show me full code including web.xml
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sivakuganAuthor Commented:
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
KuldeepchaturvediCommented:
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
sivakuganAuthor Commented:
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
KuldeepchaturvediCommented:
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
sivakuganAuthor Commented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java EE

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.