Solved

jstl attribute name

Posted on 2013-01-30
5
529 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
ID: 38837771
0
 
LVL 20

Accepted Solution

by:
Sathish David  Kumar N earned 500 total points
ID: 38838576
>>>>>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
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



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
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"/>
 </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
ID: 38873344
please advise
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to add new optional parameter to JSP 1 41
login jsp example 24 50
login form jsp example 2 25
Adding multiple JVM environments to RedHat 6 7 22
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

778 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