setattribute in jsp

In this example we are going to describe setattribute in JSP. The setAttribute method of request object.setAttribute() method we use to set an attribute to a JSP request in a web application.

setattribute in jsp

setattribute in jsp

In this example we are going to describe setattribute in JSP. The setAttribute method of request object.setAttribute() method we use to set an attribute to a JSP request in a web application. The setAttribute() method set the value of the sets the value of the attribute for the request which is retrieved later either in the current JSP Web Page. We use method getParameter() that return the value of a request parameter passed as string of request. We use  session object set these values to the session object that retrieved later in the JSPProcess.jsp web page for showing the UseName and Password message with UName and PWD by getting the session value.

Syntax of setAttribute in jsp

  • session.setAttribute( "String", values);

home.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>setattribute in jsp</title>
</head>
<h2> Example of Set Attribute in JSP</h2>
<form action="JSPProcess.jsp" method="post">
<table>
<tr>
<td>UserName:</td>
<td><input type="text" name="uname" id="uname"></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="pwd" id="pwd"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</form>
</html>

JSPProcess.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<% 
String uname = request.getParameter( "uname" );
String pwd = request.getParameter( "pwd" );
session.setAttribute( "UName", uname );
session.setAttribute( "PWD", pwd );
%>
UserName: <%= request.getAttribute( "UName" ) %><br/>
Password: <%= session.getAttribute( "PWD" ) %>
</body>
</html>

OutPut

Download Source Code