0% found this document useful (0 votes)
7 views10 pages

Session 8

The document discusses various implicit objects in JSP like request, response, out, session, application, page, config and exception. It provides examples of how to use each implicit object to retrieve and set attributes or parameters in the request or session. The document also contains examples of using response to redirect and out to print output to the response.

Uploaded by

shubhtiwari882j
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
7 views10 pages

Session 8

The document discusses various implicit objects in JSP like request, response, out, session, application, page, config and exception. It provides examples of how to use each implicit object to retrieve and set attributes or parameters in the request or session. The document also contains examples of using response to redirect and out to print output to the response.

Uploaded by

shubhtiwari882j
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 10

Request Implicit Object in JSP with examples

1. getParameter(String name) – This method is used to get the value of a request's


parameter. ...
2. getParameterNames() – It returns enumeration of all the parameter names associated to
the request. ...
3. getParameterValues(String name) – It returns the array of parameter values.

getParameter(String name)
Request Implicit Object Example
4. In the following example we are receiving the input from user in index.html page and
displaying the same information in userinfo.jsp page using request implicit object.
5. index.html
6. <html>
7. <head>
8. <title>Enter UserName and Password</title>
9. </head>
10. <body>
11. <form action="userinfo.jsp">
12. Enter User Name: <input type="text" name="uname" /> <br><br>
13. Enter Password: <input type="text" name="pass" /> <br><br>
14. <input type="submit" value="Submit Details"/>
15. </form>
16. </body>
17. </html>
18. userinfo.jsp
19. <%@ page import = " java.util.* " %>
20. <html>
21. <body>
22. <%
23. String username=request.getParameter("uname");
24. String password=request.getParameter("pass");
25. out.print("Name: "+username+" Password: "+password);
26. %>
27. </body>
28. </html>
getParameterNames()
29. <%@page import="java.util.*" %>
30. <html>
31. <head><title>getParameterNames() method of request
object.</title>
32. </head>
33. <body>
34. <form method="post">
35. <table border="0" cellspacing="0"
cellpadding="0">
36. <tr>
37. <td>User Name: </td>
38. <td><input type="text" size="20"
39. name="txtUserName" />
40. </tr>
41. <tr>
42. <td>Password: </td>
43. <td><input type="password" size=
44. "20" name="txtPassword" />
45. </tr>
46. <tr>
47. <td>&nbsp;</td>
48. <td><input type="submit" value=
49. "Submit" name="B1" /></td>
50. </tr>
51. </table>
52. </form>
53. <%
54. String ParameterNames = "";
55. for(Enumeration e =
request.getParameterNames();
56. e.hasMoreElements(); ){
57. ParameterNames =
(String)e.nextElement();
58. out.println(ParameterNames + "<br/>");
59. }
60. %>
61. </body>
62. </html>
getParameterValues(String name)

index.html
<font face="verdana" size="2px">
<form action="onGPV" method="post">
Habits :
<input type="checkbox" name="habits" value="Reading">Reading

<input type="checkbox" name="habits" value="Movies">Movies

<input type="checkbox" name="habits" value="Writing">Writing

<input type="checkbox" name="habits" value="Singing">Singing

<input type="submit" value="Submit">


</form>
</font>

onGPV.java(Servlet)
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class onGPV extends HttpServlet


{
protected void doPost(HttpServletRequest req,HttpServletResponse
res)throws ServletException,IOException
{
PrintWriter pw=res.getWriter();
res.setContentType("text/html");

String[] values=req.getParameterValues("habits");

pw.println("Selected Values...");

for(int i=0;i<values.length;i++)

pw.println("<li>"+values[i]+"</li>");

}
pw.close();
}

}
response object

In JSP the response object is implicitly defined, so you don't have to create an object. JSP
response object is created by the web container for each request of the client. It basically is used
for redirecting to any other resource.

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>GeeksforGeeks</title>
</head>
<body>
<%
//below line in JSP redirect you to www.msn.co
response.sendRedirect("https://www.msn.com/en-ing");
%>
</body>
</html>

It is an instance of javax.servlet.jsp.JspWriter. This allows user to access Servlet output stream.

Methods of out implicit object


void print(): This method writes the value that is passed into it

out.print(“Out Implicit Object in jSP….”);

void println(): This method is similar to the print() method, the only
difference between print and println is that the println() method adds a new
line character at the end.

<HTML>
<HEAD>
<TITLE> OUT IMPLICIT OBJECT EXAMPLE </TITLE>
</HEAD>
<BODY>
<%
out.print( "print statement " );
out.println( "println" );
out.print("Another print statement");
%>
</BODY>
</HTML>

session implicit object


In JSP, session is an implicit object of type HttpSession.The Java developer can use this object to set,get or remove
attribute or to get session information.
Example of session implicit object

index.html

1. <html>
2. <body>
3. <form action="welcome.jsp">
4. <input type="text" name="uname">
5. <input type="submit" value="go"><br/>
6. </form>
7. </body>
8. </html>

welcome.jsp

1. <html>
2. <body>
3. <%
4.
5. String name=request.getParameter("uname");
6. out.print("Welcome "+name);
7.
8. session.setAttribute("user",name);
9.
10. <a href="second.jsp">second jsp page</a>
11.
12. %>
13. </body>
14. </html>

second.jsp

1. <html>
2. <body>
3. <%
4.
5. String name=(String)session.getAttribute("user");
6. out.print("Hello "+name);
7.
8. %>
9. </body>
10. </html>
JSP application implicit object

In JSP, application is an implicit object of type ServletContext.

The instance of ServletContext is created only once by the web container when application or
project is deployed on the server.

This object can be used to get initialization parameter from configuaration file (web.xml). It can
also be used to get, set or remove attribute from the application scope.

Example of application implicit object:

index.html

1. <form action="welcome">
2. <input type="text" name="uname">
3. <input type="submit" value="go"><br/>
4. </form>

web.xml file

1. <web-app>
2.
3. <servlet>
4. <servlet-name>sonoojaiswal</servlet-name>
5. <jsp-file>/welcome.jsp</jsp-file>
6. </servlet>
7.
8. <servlet-mapping>
9. <servlet-name>sonoojaiswal</servlet-name>
10. <url-pattern>/welcome</url-pattern>
11. </servlet-mapping>
12.
13. <context-param>
14. <param-name>dname</param-name>
15. <param-value>sun.jdbc.odbc.JdbcOdbcDriver</param-value>
16. </context-param>
17.
18. </web-app>

welcome.jsp
1. <%
2.
3. out.print("Welcome "+request.getParameter("uname"));
4.
5. String driver=application.getInitParameter("dname");
6. out.print("driver name is="+driver);
7.
8. %>

page
This implicit object comes under java.lang.Object class. Its work is to provide reference to the
servlet class generated. Type casting is thus required to access this object as shown below.

<% (HttpServlet)page.log(“message”); %>

We use this rarely. Instead, we use it as:

Syntax:
Object page=this;
<% this.log(“message”); %>

For Example:

<html>
<head><title>Object page</title>
</head>
<body>
<%
this.log("message");
%>
</body>
</html>

Explanation: A reference page is generated over successful compilation.

JSP config implicit object

In JSP, config is an implicit object of type ServletConfig. This object can be used to get
initialization parameter for a particular JSP page. The config object is created by the web
container for each jsp page.
Generally, it is used to get initialization parameter from the web.xml file.

Example of config implicit object:

index.html

1. <form action="welcome">
2. <input type="text" name="uname">
3. <input type="submit" value="go"><br/>
4. </form>

web.xml file

1. <web-app>
2.
3. <servlet>
4. <servlet-name>sonoojaiswal</servlet-name>
5. <jsp-file>/welcome.jsp</jsp-file>
6.
7. <init-param>
8. <param-name>dname</param-name>
9. <param-value>sun.jdbc.odbc.JdbcOdbcDriver</param-value>
10. </init-param>
11.
12. </servlet>
13.
14. <servlet-mapping>
15. <servlet-name>sonoojaiswal</servlet-name>
16. <url-pattern>/welcome</url-pattern>
17. </servlet-mapping>
18.
19. </web-app>
welcome.jsp

1. <%
2. out.print("Welcome "+request.getParameter("uname"));
3.
4. String driver=config.getInitParameter("dname");
5. out.print("driver name is="+driver);
6. %>

JSP exception implicit object is only available for error pages, which means a JSP page should
have isErrorPage set to true in order to use exception implicit object.

index.html

<html>
<head>
<title>Enter two Integers for Division</title>
</head>
<body>
<form action="division.jsp">
Input First Integer:<input type="text" name="firstnum" />
Input Second Integer:<input type="text" name="secondnum" />
<input type="submit" value="Get Results"/>
</form>
</body>
</html>

Here, in division.jsp page, we have specified exception.jsp as errorPage which means if


any exception occurs in this JSP page, the control will be immediately transferred to the
exception.jsp JSP page.

Note: We have used errorPage attribute of Page Directive to specify the exception handling
JSP page (<%@ page errorPage=”exception.jsp” %>).

division.jsp
<%@ page errorPage="exception.jsp" %>
<%
String num1=request.getParameter("firstnum");
String num2=request.getParameter("secondnum");
int v1= Integer.parseInt(num1);
int v2= Integer.parseInt(num2);
int res= v1/v2;
out.print("Output is: "+ res);
%>

exception.jsp

<%@ page isErrorPage="true" %>


Got this Exception: <%= exception %>
Please correct the input data.

You might also like