Use Of PageContext() Method in Servlet Java

basically it is used to find attribute and set Attribute value on servlet :-
please find example built on your platform but please put all the file under WEB-Pages .else you will get Error :- Request resource not available
============ index.html===================
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<head>
<title> User Login Page – Enter details</title>
</head>
<body>
<form action="validation.jsp">
Enter User-Id: <input type="text" name="uid"><br>
Enter Password: <input type="text" name="upass"><br>
<input type="submit" value="Login">
</form>
</body>
</html>
=========== validation.jsp=================
<%--
    Document   : validation
    Created on : Apr 22, 2015, 4:21:40 PM
    Author     : ananddw
--%>

<html>
<head> <title> Validation JSP Page</title>
</head>
<body>
<%
String id=request.getParameter("uid");
String pass=request.getParameter("upass");
out.println("hello "+id);
pageContext.setAttribute("UName", id, PageContext.SESSION_SCOPE);
pageContext.setAttribute("UPassword", pass, PageContext.SESSION_SCOPE);
%>
<a href="display.jsp">Click here to see what you have entered </a>
</body>
</html>

==========display.jsp==============
<%--
    Document   : display
    Created on : Apr 22, 2015, 4:22:46 PM
    Author     : ananddw
--%>

<html>
<head>
<title>Displaying User Details</title>
</head>
<body>
<%
String username= (String) pageContext.getAttribute("UName", PageContext.SESSION_SCOPE);
String userpassword= (String) pageContext.getAttribute("UPassword", PageContext.SESSION_SCOPE);
out.println("Hi "+username);
out.println("Your Password is: "+userpassword);
%>
</body>
</html>
=====================================

No mapping needed on this method since it should call from web pages
.

Comments

Popular posts from this blog

Converting JSONArray into CSV file in java

Online Book Store Website Using Java

How to read CSV File in Java