jstl attribute name

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.
Who is Participating?
Sathish David Kumar NArchitectCommented:
>>>>>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 .
gudii9Author Commented:
gudii9Author Commented:
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"/>
gudii9Author Commented:
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
gudii9Author Commented:
please advise
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.