Hello Everyone,
I've a query about how servlet interacts with jsp page.
Here is what I want to do:
1. Get input from user using drop down box, the choices of the drop down come from db. I do this using input jsp page which is called from the doPost function of my servlet.
2. The user selects a choice from drop down and clicks 'Submit' button. On the click of this button, proceed to step #3.
3. Use this input, to sort data from db and then display selected data to jsp page. This jsp page is called from the doGet function of the servlet.
Now when I point my browser to http://localhost:8080/application/servlet-url, it just executes the doGet function displaying empty table. How do I make the code first call the doPost function, get the input and then pass it on to doGet function for displaying the data.
I placed my jsp file under application dir and my .class and .java files under ../application-dir/WEB-INF/classes/package-dir. I over-rided doGet function in servlet that passed the list of objects to jsp page.
input jsp page
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<HTML>
<BODY>
Please choose <BR>
<select NAME="release no">
<c:forEach var="item" items="${versionsList}">
<option>
<c:out value="${item}" />
</option>
</c:forEach>
</select>
<INPUT TYPE="SUBMIT" VALUE="Select">
</BODY>
</HTML>
servlet code
package tryjsp;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
public class DisplayServlet extends HttpServlet {
String releaseno = "3.1.50"; //my default value
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
ReleaseDataDAO myDAO = new ReleaseDataDAO();
List<String> versionsList = myDAO.getVersions();
request.setAttribute("versionsList", versionsList);
request.getRequestDispatcher("input.jsp").forward(request, response);
releaseno = request.getParameter("release no");
System.out.println("Selected release no is " + releaseno);
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
PrintWriter out = response.getWriter();
response.setContentType("text/html");
ReleaseDataDAO myDAO = new ReleaseDataDAO();
List<ReleaseData> myDataList = myDAO.getReleaseData(releaseno);
request.setAttribute("myDataList", myDataList);
request.getRequestDispatcher("output.jsp").forward(request, response);
}
}