Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 25-11-2009
Member
 
Join Date: Nov 2009
Posts: 124
Explain getAttribute method in jsp

I need a help in understanding getAttribute() method. I don't know how to pass string in the parameter. I have made program in jsp but I got a error in getAttribute method and keep on getting nullpointerexception error. Could someone solve my problem?
Reply With Quote
  #2  
Old 25-11-2009
Member
 
Join Date: May 2008
Posts: 2,293
Re: Explain getAttribute method in jsp

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>&nbsp;</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>");
%>
Reply With Quote
  #3  
Old 25-11-2009
Member
 
Join Date: Nov 2005
Posts: 1,323
Re: Explain getAttribute method in jsp

A getAttribute() of a request object is used to return value of the attribute. It returns the object that are associate with attribute. If the attribute is not present then a null value is return. If the attribute is present then return value will be the object which is associate with attribute.
syntax- request.getAttribute()
For example: Object exforsys = request.getAttribute("test");
Reply With Quote
  #4  
Old 25-11-2009
Member
 
Join Date: Oct 2005
Posts: 2,389
Re: Explain getAttribute method in jsp

getAttribute() method always returns you an object so when you used < getAttribute( .... ) > it will print some junk value on the browser. Using getAttribute() method you can call only request object. getAttribute() method always returns Object and it is used to store and retrieve the objects from session. This methods returns respective set object and if setname is not found then it return null value.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Explain getAttribute method in jsp"
Thread Thread Starter Forum Replies Last Post
Method overriding versus method hiding in C# ^MALARVIZHI^ Software Development 4 25-12-2010 06:25 AM
Explain main method in java Angelica Maria Software Development 5 10-03-2010 01:18 PM
Is it possible to call destroy() method within init() Method? Level8 Software Development 3 10-12-2009 08:36 AM
What is method overriding and method overloading in java beelow Software Development 3 17-11-2009 08:20 AM
Java: How can I call one method in another method? biohazard 76 Software Development 3 16-07-2009 07:12 PM


All times are GMT +5.5. The time now is 05:52 AM.