Solved

jstl attribute name

Posted on 2013-01-30
5
522 Views
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
</jsp:useBean>


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
</jsp:useBean>


something like below

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



I have not understood ignoring, execution part.




Also


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

Hello<%=user.getUserName()%>

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.
0
Comment
Question by:gudii9
  • 4
5 Comments
 
LVL 7

Author Comment

by:gudii9
Comment Utility
0
 
LVL 20

Accepted Solution

by:
Sathish David  Kumar N earned 500 total points
Comment Utility
>>>>>I have not understood ignoring, execution part.

 
its something like same object you going to create again .

eg:
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:useBean>

<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 .
0
 
LVL 7

Author Comment

by:gudii9
Comment Utility
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



LoinServlet.java

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() {
            super();
      }

      /**
       * 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;

            userId=request.getParameter("userId");
            password=request.getParameter("password");
            
            LoginService loginService=new LoginService();
            boolean result=loginService.authenticate(userId,password);
            if(result){
                  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");
                  //response.sendRedirect("success.jsp");
                  return;
                  
            }
            
            else{
                  System.out.println("inside login servlet else");
                  response.sendRedirect("Login.jsp");
                  return;
                  
            }
      }

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

}


success.jsp

<%@ 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">
<html>
  <head>
    <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">
      -->

  </head>
 
  <body>
<h3>login success</h3>

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

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



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"/>
loinJspChange.jpg
SuccessJspChange.jpg
0
 
LVL 7

Author Comment

by:gudii9
Comment Utility
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"/>
 </jsp:useBean>


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();
            user.setUserName(users.get(userID));
            user.setUserId(userID);
            return user;
            
      }

}


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

Author Comment

by:gudii9
Comment Utility
please advise
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
bigHeights  challenge 13 55
strCopies  challenge 17 73
eclipse package explorer vs project explorer view 2 27
maven project error 5 17
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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 …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
The viewer will learn how to implement Singleton Design Pattern in Java.

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now