Results 1 to 4 of 4

Thread: Explain getAttribute method in jsp

  1. #1
    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?

  2. #2
    Join Date
    May 2008
    Posts
    2,297

    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>");
    %>

  3. #3
    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");

  4. #4
    Join Date
    Oct 2005
    Posts
    2,393

    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.

Similar Threads

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

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,689,756.69632 seconds with 17 queries