Session 8
Session 8
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> </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
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;
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>
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>
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
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.
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.
Syntax:
Object page=this;
<% this.log(“message”); %>
For Example:
<html>
<head><title>Object page</title>
</head>
<body>
<%
this.log("message");
%>
</body>
</html>
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.
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>
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