getAttribute() method gets the values corresponding to the given attribute name which is set through the setAttribute() method of the request object. The getAttribute() method of the object takes a string argument in a form of attribute name which values is to be retrieved.
HTML file
HTML Code:
<html>
<body>
<form action="GetAttributeMethod.jsp" method="post">
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td>User Name: </td>
<td><input type="text" size="30" name="txtUserName" />
</tr>
<tr>
<td>Password: </td>
<td><input type="password" size="30" name="txtUserName" />
</tr>
<tr>
<td> </td>
<td><input type="submit" value="Submit" name="B1" /></td>
</tr>
</table>
</form>
</body>
</html>
jsp code getAttributemethod.jsp file
Code:
<%@page import="java.util.*" %>
<%
String user, password;
if(request.getParameter("txtUser") == null)
user = "";
else
user = request.getParameter("txtUser");
if(request.getParameter("txtPassword") == null)
password = "";
else
password = request.getParameter("txtPassword");
request.setAttribute("UName", user);
%>
<%
String strViewPage="GetAttributeMethod1.jsp";
RequestDispatcher dispatcher = request.getRequestDispatcher(strViewPage);
if (dispatcher != null){
dispatcher.forward(request, response);
}
%>
JSP code of the GetAttributeMethod1.jsp file
Code:
<%
out.println("<b>Welcome " + request.getAttribute("User") + "!</b>");
%>
Bookmarks