Home » Java programming language

JSP - How to use bean action tag with session scope?

By: Vanka Manikanth, on 17 JAN 2017

JSP - Use bean is used to locate or instantiate the bean class. If the bean is already created while it is not created again else it would create and instantiate. Here we can set the properties of use bean scope to request, session, page and application.

Scope: Session

By keeping the scope to session portrays that the fields which is input by the user are kept in session. These values can be utilized throughout the session.

Below is the example to explain briefly the Use bean tag:

This is where we get the input values from the user and these values are action to UseBean.jsp

Index.jsp

This is a welcome form, from where we will take the values for registration from the user.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"pageEncoding="ISO-8859-1"%>  
<!DOCTYPE html>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
		<title>JSP ACTION TAGS EXAMPLE</title>
		<style>
			.formsubmit{
			border:2px solid #000;
			padding:20px;
			}
		</style>
	</head>
	<body>
		<h1>Jsp UseBean Action Tag Example</h1>
		<div class="formsubmit">
		<form action="useBean.jsp" method="post"><!--The values input are triggered to UseBean.jsp  -->
			ID:<input type="text" name="id">
			USERNAME:<input type="text" name="username">
			EMAIL:<input type="email" name="email">
			GENDER:<select name="gender" required> <!--input the values from user  -->
			<option value="">Select</option>
			<option value="Male">Male</option>
			<option value="Female">Female</option>
			</select>
			<button>SESSION THESE</button>
		</form>
		</div>
	</body>
</html>

CREATE PACKAGE com.jspactiontags.example

CREATE CLASS Usebean.java

Usebean.java

Here we are setting all the properties which are input by the user.

package com.jspactiontags.example;

public class Usebean {
	private int id; //setting up the values to setters and getters
	private String username,email,gender;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getGender() {
		return gender;
	}
	public void setGender(String gender) {
		this.gender = gender;
	}
}

UseBean.jsp

Here we use jsp:UseBean action tag in order to set the properties to scope.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
		<title>UseBean JSP</title>
	</head>
	<body>
		<%
			try {
		%>
		<!-- try and catch block is for handling exception, cause user may enter the details as he like without following any contraints,in that case the exception is handled -->
		<jsp:useBean id="uObject" class="com.jspactiontags.example.Usebean"
			scope="session"></jsp:useBean>
		<!-- use bean id is for name, provide fully qualified class name and put the scope to session -->
		<jsp:setProperty property="*" name="uObject" />
		<!-- setting the properties to Use bean Class -->
		<h1>WELCOME ${param.username}</h1>

		<a href="sessionplace.jsp">Click Here</a> to View Details :
		<%
			} catch (Exception e) {
				//if exception occurs this block will execute
				//to execute this block enter the user id in characters
				out.print("<p>Invalid User Details</p>");
				request.getRequestDispatcher("index.jsp").include(request, response);
			}
		%>
	</body>
</html>

Sessionplace.jsp

Utilize the scope properties which are in session.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
		<title>Properties Scope</title>
	</head>
	<body>
		<h1>Details are :</h1>
		<!-- sessioned in useBean.jsp are utilized here -->
		<jsp:useBean id="uObject" class="com.jspactiontags.example.Usebean" scope="session"></jsp:useBean>
		<!-- getting all the properties which are kept in session -->
		<h3>USER ID :<jsp:getProperty property="id" name="uObject"/></h3>
		<h3>USER NAME: <jsp:getProperty property="username" name="uObject"/></h3>
		<h3>EMAIL ID:<jsp:getProperty property="email" name="uObject"/></h3>
		<h3>GENDER:<jsp:getProperty property="gender" name="uObject"/></h3>

		<!-- TO SEE THE SESSIONED PROPERTIES, COPY THE URL OF SESSIONPLACE.JSP(WHEN EXECUTED) AND OPEN A NEW TAB IN THE SAME BROWSER
		AND PASTE AND GO WITH THE URL
		 -->
	</body>
</html>

WEB.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>JspActionTags</display-name>
  <welcome-file-list>        
    <welcome-file>index.jsp</welcome-file>    
  </welcome-file-list>
</web-app>

OUTPUT

JSP - How to use bean action tag with session scope?

JSP bean action tag

JSP bean action tag with session scope





Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.