want to create a application using jsp pages such that it takes a value from user in a textbox and on the basis of that value it retrives other values from database and fill other textboxes with that retrived values
A:
Create a HTML form and put it in
search.jsp
:<form action="search" method="post"> <input type="text" name="query"> <input type="submit"> </form>
Create a Java class which
extends HttpServlet
and does the desired business task:protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String query = request.getParameter("query"); Data data = someDAOClass.find(query); request.setAttribute("data", data); // Will be available in EL by ${data} request.getRequestDispatcher("search.jsp").forward(request, response); // Return back to JSP page. }
Map this servlet on an
url-pattern
of/search
inweb.xml
so that it will be executed then the form in the JSP is submitted.Extend the form with the input fields which should display this data. Just fill the
value
attribute of the input elements with the desired information.<form action="somethingelse" method="post"> <input type="text" name="id" value="${fn:escapeXml(data.id)}" /> <input type="text" name="name" value="${fn:escapeXml(data.name)}" /> <input type="text" name="email" value="${fn:escapeXml(data.email)}" /> <input type="text" name="age" value="${fn:escapeXml(data.age)}" /> ... </form>
The
fn:escapeXml
is by the way there to prevent XSS.
BalusC
2010-05-06 12:22:10