Solved

jstl attribute name

Posted on 2013-01-30
5
537 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

734 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