20 - JSP Standard Tag Library (JSTL): Page 3 of 7

Now I have created another web application with name “HelloWorld” and created one jsp login.jsp in HelloWorld web application

login.jsp

<html>
  <head>
    <title>Login Form</title>
  </head>
  <body>
    <form name="logonform" method="POST">
      Username: <input type="text" name="username"/>
      <br/>
      Password:<input type="password" name="password"/>
      <br/>
      <input type="submit" value="Submit"/>
    </form>
  </body>
</html>

Now change credirect.jsp to use both Url and context attribute to redirect to login.jsp created in HelloWorld web application.

<html>
  <head>
   <title> c redirect tag Example </title>
  </head>
   <%@ taglib prefix="c"uri="http://java.sun.com/jsp/jstl/core" %>
  <body>
   <c:redirecturl="/login.jsp" context="/HelloWorld"></c:redirect>    
  </body>
</html>

Access credirect.jsp by hitting http://localhost:8080/jsp-tutorial/credirect.jsp . You will see that page is redirected to login.jsp of HelloWorld web application

20.3.4 <c:set>

 The <c:set> tag is used to this tag is used to set the variable value in given scope. This tag can also be used to set the bean properties.The syntax and attributes are as follows:

<c:set var=”variable_name” scope=”[page| application | session| request]” value=”value of variable” target=”object whose property to be modified” property=”property of target which needs to modify”/>

· value- is use to specify the value

· var- name of the variable to store the information.

· scope- scope in which variable will be created. Possible values can be request, application ,session and page. Default scope is page

· target- target bean whose property to modify

· property- property which needs to be modified

Note :

If target is specified, property must also be specified.

If value is set to null, variable will be removed.

Example-

Create Person.java in com.sample.jsp.tutorial package like below

packagecom.sample.jsp.tutorial;
publicclass Person {
    private String firstName="Not Provided";
    private String lastName="Not Provided";
    public String getFirstName() {
        returnfirstName;
    }
    publicvoidsetFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {
        returnlastName;
    }
    publicvoidsetLastName(String lastName) {
        this.lastName = lastName;
    }
}

Lets create cset.jsp to demonstrate the <c:set> tag like below

<html>
  <head>
    <title> c:set tag example </title>
  </head>
  <%@ taglib prefix="c"   uri="http://java.sun.com/jsp/jstl/core" %>
  <body>
    <c:setvar="pageVariable" value="Variable in Page Scope" />
    Page Scoped Variable :: <c:out value="${pageVariable}"/>
    <br/>
    <c:setvar="sessionVariable" value="Variable in Session Scope"  scope="session"/>
    Session Scoped Variable :: <c:out value="${sessionVariable}"/>
    <br/>
    <jsp:useBean id="person" class="com.sample.jsp.tutorial.Person" scope="request"/>
    <c:set target="${person}" property="firstName" value="Test" />
    Value of First Name :: <c:out value="${person.firstName}"/>
  </body>
</html>

Access cset.jsp by hitting http://localhost:8080/jsp-tutorial/cset.jsp .

 

 

 

 

 

 

 

Like us on Facebook