0

Intro: I am a bit of a noob to the relationship between doGet and doPost in servlets.

Scope: I am building a tool to help me with an online auction site where:

  1. I (the user) enter a name into a form (html page), this name will be a URL (potentially for sale ->business method determines this through an API)
  2. the html form posts the form name in doPost and then doPost uses the form name as an argument to call a business method
  3. after the call to business method I redirect to a results html/js page that uses Ajax to call the doGet method to get the associated output (a public class String populated in business logic method)

Problem: The Ajax page does not seem to get the output from doGet (actually doGet does not seem to have the String to give, no errors- just blank like String="". Which it is until business logic method adds to it).

Question 1: How can I use doPost to request the form String 'st' so that I may call the business method while also redirecting to the html/js results page AND also am able to call doGet from Ajax

Question 2: I've been trying to solve my answer by reading SO and other sites- but would like to formally ask rather than impute: is the use of a servlet the fastest way to achieve the Scope(above)? As opposed to JSPs or any other java server side libraries/ frameworks?

hw/ sw: CentOS 6.3, 16gb ram, physical node, corei7 @3.2, container is tomcat 7

HTML

<html>
<head>
<title>URL Search Page</title>
</head>
<body>
<CENTER>
 <FORM ACTION="/ResultServlet/Results" METHOD=GET>
   <INPUT TYPE=TEXT NAME="st">
   <INPUT TYPE=SUBMIT VALUE=Submit>
  </FORM>
</CENTER>
</body>
</html>

Servlet

@WebServlet("/Results")
public class Results extends HttpServlet {
    private static final long serialVersionUID = 1L;
       public static String str="";

    public static void businessLogic(String q){
        try {
            str = new compute.URL.GetAvailURI( "https://www.registerdomains.com/auctionAPI/Key:a05u3***1F2r6Z&urlSearch="+q);
            /*more boring number crunching */
            }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter printWriter  = response.getWriter();
        printWriter.println(str);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String st = request.getParameter("st");
        businessLogic(st);
        response.sendRedirect("results/resultActionURL.html");

    }

}
Peter Elliott
  • 3,273
  • 16
  • 30
Chris
  • 18,075
  • 15
  • 59
  • 77
  • does something actually call the `businessLogic` method? If nothing ends up calling it, then str will be blank when `doGet` reads it – Peter Elliott Jan 16 '13 at 04:26
  • thank you for your time, yes... the second line in doPost calls the business method – Chris Jan 16 '13 at 04:27

3 Answers3

3

One problem you form methos is get so when you submit the form it will run the doget method in the servlet which does nothing in your case.

so first change the method to post then try running, also post the code of the html page on which you have written the ajax code.

I think so you are calling the same servlet from the ajax method but at that time the value in the str wont remain so append the needed data as query string while redirecting to

response.sendRedirect("results/resultActionURL.html?st="+ st);

This value you can get from using javascript

 function getParameterByName(name) {
                name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
                var regexS = "[\\?&]" + name + "=([^&#]*)";
                var regex = new RegExp(regexS);
                var results = regex.exec(window.location.search);
                if (results == null)
                    return "";
                else
                    return decodeURIComponent(results[1].replace(/\+/g, " "));
 }
 var st=getParameterByName(st);
 //add your ajax call code and pass st as data there.

Hope this answers your question.

Meherzad
  • 8,433
  • 1
  • 30
  • 40
1

You are making your application stateful by doing this. You are storing data in server across requests.

That means your logic depends on the fact that the same servlet object is triggered, in sequence

  1. Post request - calls business method and populates a string
  2. Get request - tries to retrieve the string populated in last request.

First thing is this is not needed at all, you can pass the populated String to browser during redirect from doPost itself as a parameter or cookie value.

Even if you want to do this store this String in session request.getSession().setAttribute("str", populated string)

and in doGet method retrieve that using

request.getSession().getAttribute("str"

This much better than having an instance variable in your servlet,.

Subin Sebastian
  • 10,870
  • 3
  • 37
  • 42
1

Once you redirect to other page servlet will destroy request and response objects and variable values... so you can't get the old one... So try to get before redirect or save it on session object...

Kanagaraj M
  • 956
  • 8
  • 18