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:
- 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)
- the html form posts the form name in doPost and then doPost uses the form name as an argument to call a business method
- 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");
}
}