Go Premium for a chance to win a PS4. Enter to Win


jstl attribute name

Posted on 2013-01-30
Medium Priority
Last Modified: 2013-03-13
I was reading about jstl.

User user=(User)request.getAttribute("user");

Above line can be represented as below.

 <jsp:useBean id="user" class="org.gp.gpr.dto.User" scope="request"></jsp:useBean>

If the object is already there then any code written in between  <jsp:useBean start and end tag ignored
<jsp:useBean id="user" class="org.gp.gpr.dto.User" scope="request">

//object is already there  code here ignored

If object is not already there then any code written in between  <jsp:useBean start and end tag is executed
<jsp:useBean id="user" class="org.gp.gpr.dto.User" scope="request">

//object is not already there code here not ignored but executed

something like below

<jsp:useBean id="user" class="org.gp.gpr.dto.User" scope="request">
 <jsp:setProperty property="userName" name="user" value="TestUSers"/>

I have not understood ignoring, execution part.


In the above declaration JSTL assumes attribute name 'user' same as obect name. That is reason we cannot give same attribute name for request, session etc since while getting


i mean in jstl way as below

Hello<jsp:getProperty property="userName" name="user"/>
code does not understand whether to use 'user' from request or session

 I have not understood this concept clearly what it means.

Please advise. Any ideas, sample code, resources highly appreciated. Thanks in advance.
Question by:gudii9
  • 4

Author Comment

ID: 38837771
LVL 20

Accepted Solution

Sathish David  Kumar N earned 2000 total points
ID: 38838576
>>>>>I have not understood ignoring, execution part.

its something like same object you going to create again .

String a =new String();

if again you will create   Same object String a=new String () it will give error . the same think happen in JSTL but its ignorning .

<jsp:useBean id="user" class="org.gp.gpr.dto.User" scope="request">
           <jsp:setProperty property="userName" name="user" value="TestUSers"/>

<jsp:getProperty property="userName" name="user"/>

Open in new window

your setting value there and get the same value below .

while your define bean as  request only .

if you dont want to set the value you can set throw java and pass it here .

Author Comment

ID: 38840478
i chaned the attribute name to 'userss' instead of 'user' in LoinServlet.java

Ad accordinly i changed the success.jsp code to retrieve as below


package org.gp.gpr;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.gp.gpr.dto.User;
import org.gp.gpr.service.LoginService;

public class LoginServlet extends HttpServlet {

       * Constructor of the object.
      public LoginServlet() {

       * Destruction of the servlet. <br>
      public void destroy() {
            super.destroy(); // Just puts "destroy" string in log
            // Put your code here

       * The doPost method of the servlet. <br>
       * This method is called when a form has its tag value method equals to post.
       * @param request the request send by the client to the server
       * @param response the response send by the server to the client
       * @throws ServletException if an error occurred
       * @throws IOException if an error occurred
      public void doPost(HttpServletRequest request, HttpServletResponse response)
                  throws ServletException, IOException {
            String userId,password;

            LoginService loginService=new LoginService();
            boolean result=loginService.authenticate(userId,password);
                  User user= loginService.getUserDetails(userId);
                  request.setAttribute("userss", user);
                  RequestDispatcher dispatcher=request.getRequestDispatcher("success.jsp");
                  dispatcher.forward(request, response);
                  System.out.println("inside login servlet");
                  System.out.println("inside login servlet else");

       * Initialization of the servlet. <br>
       * @throws ServletException if an error occurs
      public void init() throws ServletException {
            // Put your code here



<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1" import="org.gp.gpr.dto.User"%>
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <base href="<%=basePath%>">
    <title>My JSP 'success.jsp' starting page</title>
      <meta http-equiv="pragma" content="no-cache">
      <meta http-equiv="cache-control" content="no-cache">
      <meta http-equiv="expires" content="0">    
      <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
      <meta http-equiv="description" content="This is my page">
      <link rel="stylesheet" type="text/css" href="styles.css">

<h3>login success</h3>

 <jsp:useBean id="userss" class="org.gp.gpr.dto.User" scope="request">
 <jsp:setProperty property="userName" name="userss" value="TestUSers"/>

 helloo<jsp:getProperty property="userName" name="userss"/>
  helloo<jsp:getProperty property="userId" name="userss"/>

But after the chane some reason i am getting null when accessing

  helloo<jsp:getProperty property="userId" name="userss"/>

after setting

<jsp:setProperty property="userName" name="userss" value="TestUSers"/>

Please advise on how to avoid null.

In what situations we set property as below

<jsp:setProperty property="userName" name="userss" value="TestUSers"/>

Author Comment

ID: 38840505
If i declare like below

 <jsp:useBean id="userss" class="org.gp.gpr.dto.User" scope="request">
 <jsp:setProperty property="userName" name="userss" value="TestUSers"/>

then there is no need of writin DTO class 'User.java' as below

package org.gp.gpr.dto;

public class User {
      private String userName;
      private String userId;
      public String getUserName() {
            return userName;
      public void setUserName(String userName) {
            this.userName = userName;
      public String getUserId() {
            return userId;
      public void setUserId(String userId) {
            this.userId = userId;


Please advise

Also what is the purpose of LoinService.java

package org.gp.gpr.service;

import java.util.HashMap;

import org.gp.gpr.dto.User;

public class LoginService {
      HashMap<String, String> users=new HashMap<String, String>();
      public LoginService(){
            users.put("mk", "mike");
            users.put("ek", "erik");
            users.put("pl", "paul");
      public boolean authenticate(String userId, String password){
            if(password == null||password.trim() ==""){
                  return false;
            return true;
      public User getUserDetails(String userID){
            User user=new User();
            return user;


why are we settin User object within getUserDetails() method. I am not clear on this flow. Please advise

Author Comment

ID: 38873344
please advise

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

782 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question