JSP
JSP
<html>
<body>
<%! int data = 50; %>
<%="Value of the variable is:"=data %>
</body>
</html>
<html>
<body>
<%! int cube(int n){ return n*n*n;} %>
<%="Cube of 3 is:"=cube(3) %>
</body>
</html>
There Are 9 JSP implicit objects. These objects are created by the web container that are available to all
the jsp pages.
The available implict objects are out , request , config , session , application etc.
1. OUT - jspWriter
2. Request - HttpServletRequest
3. Response - HttpServletResponse
4. Config - ServletConfig
5. application - ServletContext
6. Session - HttpSession
7. pageContext - pageContext
8. page - Object
9. Exception - Throwable
The JSP Request ia an implicit object of type HttpServletRequest i.e. created for each jsp request by the
web container. It can be used to get request information such as parameter, header information , remote
address, server name, server port, content type , character encoding etc.
<form action="welcome.jsp">
<input type="text" name="uname">
<input type="submit" value="go">
</form>
welcome.jsp
-----------
<%
String name=request.getParameter("uname");
out.print("welcome"+name);
%>
<form action="welcome.jsp">
<input type="text" name="uname">
<input type="submit" value="go">
</form>
welcome.jsp
-----------
<%
response.sendRedirect("https://www.serverxperts.com");
%>
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.
<form action="welcome.jsp">
<input type="text" name="uname">
<input type="submit" value="go">
</form>
welcome.jsp
-----------
<%
String name=request.getParameter("uname");
out.print("welcome"+name);
session.setAttribute("user",name);
<a href="second.jsp">Second jsp Page</a>
%>
Second.jsp
<%
session.setAttribute("user");
out.print("Hello"+name);
%>
# JSP application implicit object
---------------------------------
In JSP , application is an implicit object of type ServletContext. The instance of ServerContext is created
only once by the web container when application or project is deployed on the server.
<form action="welcome.jsp">
<input type="text" name="uname">
<input type="submit" value="go">
</form>
web.xml file
<web-app>
<servlet>
<servlet-name>Rohan Kumar</servlet-name>
<jsp-file>/welcome.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>Rohan Kumar</servlet-name>
<ur!-pattern>/welcome</ur!-pattern>
</servlet-mapping>
<context-paran>
<param-name>dname</param-name>
<param-value>sun.jdbc.odbc.jdbcodbcDriver</param-value>
</context-param>
</web-app>
welcom.jsp
<%
out.print(welcome"+request.getParameter("uname"));
String driver=application.getInitParameter("dname");
out.print("driver name is="+driver);
%>