Param-Ganak
Param-Ganak

Reputation: 5875

How to fill the html form in jsp with the dispatched data from servlet?

I am new to JSP and Servlet. I need your suggestion in following task. I have a jsp page which hava a html form which accepts some information from user.

JSP page info.jsp

<%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<%
    if(request.getAttribute("reply")!=null){
        out.println(request.getAttribute("reply"));
    }
%>

<form action="actionservlet" method="post" name="myform">
user Name:<input name="username" type="text" size="10" /><br />
First Name:<input name="username" type="text" size="10" /><br />
Last Name:<input name="username" type="text" size="10" /><br />
Email id:<input name="username" type="text" size="10" /><br />
<input name="submit" type="submit" value="Submit" />
<input name="Reset" type="reset" value="Reset" />

</form>
</body>
</html>

As this client enters information and submits the form the data in the form is set to actionservlet which check whether the username entered by user is already exists in database. If username already exists then it sends the data back to the calling jsp and with an extra attribute that is "reply" and value as appropriate message. i.e. "user already exists " if entered user name is already exists in database or "user information saved successfully!" if data saved successfully.

I want to do something like that Whenever the username which is already exists is entered by user and form submitted then in this case the all data send back to the jsp should be get fill to their respective textbox. So that user can only enter the other username and submit this form again. How to achieve the above task.

I am thinking of adding jsp scriplet to value property of each textbox and check that whether this replay message. if the reply message is regarding duplicate username then it should assign the value to value property else the value property will remain empty. But I think that this solution is not optimum because as the number of fields will increases in the form this solution will get little bit tedious. And also I think due to this the it give rise to some sort of redundant code. So I want to ask experts that is there any other way to do the same. I want to fill the form only when the error or duplicate message comes and not when success message comes.

Thank You!

Upvotes: 3

Views: 13580

Answers (1)

Hardik Mishra
Hardik Mishra

Reputation: 14897

Using Java code in JSP is a bad practice and Scriptlets are outdated, you should avoid using it.

JSP 2.0 Expression Language is the solution. Download jstl.jar and add it to your classpath.

JSP

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<div><b>${reply}</b></div>
<form action="actionservlet" method="post" name="myform">
user Name:<input name="username" type="text" size="10" /><br />
First Name:<input name="firstName" type="text" size="10" value=${firstName}/><br />
Last Name:<input name="lastName" type="text" size="10" value=${lastName}/><br />
Email id:<input name="email" type="text" size="10" value=${email}/><br />
<input name="submit" type="submit" value="Submit" />
<input name="Reset" type="reset" value="Reset" />
</form>

Get parameters in the Servlet. Perform operation/task you want to do. Set request attributes in your Servlet. Forward page to JSP.

Servlet:

String firstName = request.getParameter("firstName");
// and so on get other form parameters..
// perform your opeations

request.setAttribute("reply", "User Already Exists"); // Just an example
request.setAttribute("firstName", firstName);
// set other form parameters..

RequestDispatcher dispatcher =
request.getRequestDispatcher("/el/scoped-vars.jsp");
dispatcher.forward(request, response);

Check more on this link if Expression Language does not work.

Read basic Expression Language : http://pdf.coreservlets.com/JSP-EL.pdf

Upvotes: 8

Related Questions