Implicit Objects are the Java objects that the JSP Container makes available to the developers on each page, and we can call them directly without being explicitly declared. They are also called pre-defined variables. It can be new because there is no implicit objects in Java. There are nine implicit objects in JSP that you will see below : 

Object Actual Type Description
request java.servlet.http.HttpServletRequest/ 
javax.servlet.ServletRequest
Request information like a parameter, header information, server name, etc.
response javax.servlet.http.HttpServletResponse/ 
javax.servlet.ServletResponse
to content type, add cookie and redirect to response page
pageContext javax.servlet.jsp.PageContext to get, set and remove the attributes from a particular scope
session javax.servlet.http.HttpSession to get, set and remove attributes to session scope and also used to get session information
application javax.servlet.ServletContext to interact with the servlet container
out javax.servlet.jsp.jspWriter to access the servlet’s output stream
config java.servlet.servletConfig to get the initialization parameter in web.xml
page java.lang.Object Page implicit variable holds the currently executed servlet object for the corresponding jsp.
exception java.lang.Throwable It is used for exception handling in JSP.

These methods are available to the JSP implicit request object. In Model1, we don't use String getRequestURI() and String getContextPath() as much as in Model2.

Method Description
setCharacterEncoding(String env) processes encoding Korean.
Object getAttribute(String name) returns the value of the attribute named name.
Enumeration getAttributeNames() returns the name of all attributes in an HTTP request.
String getParameter() returns the value of request parameter as String or null if the parameter doesn't exist.
Enumeration getParameterNames() returns the names of all parameters in request.
String getContentType() returns the MIME type of the request or null for an unknown type.
String getProtocol() returns the name of the protocol and its version used by the request.
String getRemoteAddr() returns the IP address that the request came from.
String getServerName() returns the name of Server that received the request.
int getServerPort() returns Server port at which the request is received.
boolean isSecure() returns true if the request came from HTTPS protocol and false if it didn't.
Cookies getCookies() returns an array of Cookie objects that came along with this request.
String getMethod() returns the name of the method(GET, PUT, POST) using which the request was made.
String getRequestURI() returns the URI of the page that initiated the request.
String getContextPath() returns the context path(project name).

Let's see if we can find the methods on Java EE Api.

https://docs.oracle.com/javaee/7/api/toc.htm

 

Java(TM) EE 7 Specification APIs

 

docs.oracle.com

For example, you will click the package and the interface to find the methods in the request object. 

 

Request Example1 

<HTML>

<html>
<head>
	<meta charset="UTF-8">
	<title>request Example1</title>
	<script src="http://code.jquery.com/jquery-latest.js"></script>
	<script>
	$(document).ready(function(){
		$("form").submit(function(){
			if($("#name").val()==""){
				alert("Please insert your name.");
				$("#name").focus();
				return false;
			}
			if($("#studentNum").val()==""){
				alert("Please insert your student number.");
				$("#studentNum").focus();
				return false;
			}
			if($("#male").is(":checked")==false &&
			   $("#female").is(":checked")==false){
				alert("Please select your sex.");
				return false;
			}
			if($("#major").val()=="#"){
				alert("Please select your major.");
				$("#major").focus();
				return false;
			}
		});	
	});	
	</script>
</head>
<body>
<h1>Request Example1</h1>

<form name=myform method=post action="requestExample1.jsp">
 Name : <input type="text" name="name" id="name" autofocus="autofocus"><br>
 Student No. : <input type="text" name="studentNum" id="studentNum"><br>
 Sex : Male <input  type="radio" name="sex"  value="male" id="male">
      Female <input type="radio" name="sex" value="female" id="female"><br>
      Prefer not to say <input type="radio" name="sex" value="prefernottosay" id="prefernottosay"><br>
 Major : <select name="major" id="major">
			<option value="#">Select your major</option>
			<option value="CS">Computer Science</option>
			<option value="EL">English Literature</option>
			<option value="M">Mathmetics</option>
			<option value="PS">Political Science</option>
			<option value="BM">Business Management</option>
		</select><br>
<input type="submit" value="submit">
<input type="reset" value="cancel">
</form>

</body>
</html>

<JSP>

<%@ page contentType="text/html;charset=utf-8"%>

<%	
	request.setCharacterEncoding("utf-8");
%>

<html>
<h1>Request Example1</h1>

<%
	String name = request.getParameter("name");
	String studentNum = request.getParameter("studentNum");
	String sex = request.getParameter("sex");
	String major = request.getParameter("major");
/* 
	if(sex.equals("m")){
		sex = "male";
	}else{
		sex = "female";
	} */
%>

<body>
Name: <%=name%><p>
Student No.: <%=studentNum%><p>
Sex : <%=sex%><p>
Major : <%=major%>
</body>
</html>

When you click submit, you will see this.

There is a difference between the two methods: get and post. If you use the post method, you will not see anything on the URL; with the get method, you will see the input data on the URL. Because of security reasons, we usually use the post method. 

 

Request Example2

<HTML>

<html>
	<head><title>Your hobbies</title>	
	<meta charset="utf-8">
	<script src="http://code.jquery.com/jquery-latest.js"></script>
	<script>
	$(document).ready(function(){
//		$("form").submit(function(){
		$("#myform").submit(function(){
			/* var cnt=0;
			if($("#h1").is(":checked"))	cnt++;
			if($("#h2").is(":checked"))	cnt++;
			if($("#h3").is(":checked"))	cnt++;
			if($("#h4").is(":checked"))	cnt++;
			if($("#h5").is(":checked"))	cnt++;
			if($("#h6").is(":checked"))	cnt++;
			if($("#h7").is(":checked"))	cnt++;
			
			if(cnt < 2){
				alert("Select more than one.");
				return false;
			} */
			
			if($("input:checkbox[name='site']:checked").length < 2){
				alert("Select more than one.");
				return false;
			}			
			
		});
	});	
	</script>
	</head>
<body>

<form id="myform" name=myform  method=post action="check.jsp"> 

	Select your hobbies.<br><br> 
	<input type="checkbox" id="h1" name="site" value="Walking">Walking<br>
	<input type="checkbox" id="h2" name="site" value="Swimming">Swimming<br>
	<input type="checkbox" id="h3" name="site" value="Reading">Reading<br>
	<input type="checkbox" id="h4" name="site" value="Programming">Programming<br>
	<input type="checkbox" id="h5" name="site" value="Watching Movies">Watching Movies<br>
	<input type="checkbox" id="h6" name="site" value="Climbing">Climbing<br>
	<input type="checkbox" id="h7" name="site" value="Running">Running<br><br>

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

</form> 

</body>
</html>

<JSP>

<%@ page contentType="text/html;charset=utf-8"%>

<html>
<body>

	<%
		request.setCharacterEncoding("utf-8");

		String[] choice = request.getParameterValues("site");
		String result = "";

		for (int i = 0; i < choice.length; i++) {
			result = result + choice[i] + " ";
		}
	%>

	<center>
		You like <font color=blue><%=result%></font>!
	</center>

</body>
</html>

Request Example3

<HTML>

<html>
	<head><title>radio button</title>
	<meta charset="utf-8">
	<script src="http://code.jquery.com/jquery-latest.js"></script>
	<script>
	$(function(){
		$("form").submit(function(){
			/* if($("#s1").is(":checked")==false &&
			   $("#s2").is(":checked")==false &&
			   $("#s3").is(":checked")==false &&
			   $("#s4").is(":checked")==false ){
				alert("Choose your favorite season.");
				return false;
			} */

				if($("input:radio[name='season']:checked").length<1){
					alert("Choose your favorite season.");
					return false;
				}
		});
	});	
	</script>
	</head>
	<body>
	Choose your favorite season.
	<p>

	<form name=syberpoll method=post action=result.jsp>
		<input type=radio id="s1" name=season value=Spring>Spring<br>
		<input type=radio id="s2" name=season value=Summer>Summer<br>
		<input type=radio id="s3" name=season value=Fall>Fall<br>
		<input type=radio id="s4" name=season value=Winter>Winter<br>
		<input type=submit value="투표">
	</form>

	</body>
</html>

<JSP>

<%@ page contentType="text/html;charset=utf-8" %>

<html>
	<head><title>Survey</title>
	</head>
	<body>

	<%
		request.setCharacterEncoding("utf-8");
	
		String choiceseason = request.getParameter("season");
		String result = "";

		/* if(choiceseason.equals("spring")){
			result = "Spring";
		} else if(choiceseason.equals("summer")){
			result = "Summer";
		} else if(choiceseason.equals("autumn")){
			result = "Fall";
		} else if(choiceseason.equals("winter")){
			result = "Winter";
		} */
	%>

		Your favorite season is <%=choiceseason%> !
	</body>
</html>

Request Example4

This example is to know the basic information of the client and server. Amongst them, we use getRemoteAddr(), getRequestURI(), getContextPath() the most. 

<JSP>

<%@ page contentType = "text/html; charset=utf-8" %>

<html>
<head><title>Client/ Server Information</title>
<meta charset="utf-8">
</head>
<body>

Client IP = <%= request.getRemoteAddr() %> <br>
Requested get content length = <%= request.getContentLength() %> <br>
Requested data Encoding = <%= request.getCharacterEncoding() %> <br>
Requested data content type = <%= request.getContentType() %> <br>
Requested data Protocol = <%= request.getProtocol() %> <br>
Reqeusted data transition method = <%= request.getMethod() %> <br>
Requested URI = <%= request.getRequestURI() %> <br>
Path of Context = <%= request.getContextPath() %> <br>
Server name= <%= request.getServerName() %> <br>
Server port = <%= request.getServerPort() %> <br>

</body>
</html>

Instead of localhost, insert your IP address to see the information. To find your IP address, in the command prompt, insert ipconfig.

Request Example5

<makeTestForm.jsp>

<%@ page contentType="text/html; charset=utf-8"%>

<html>
<head>
<title>Form</title>
</head>
<body>

	Please fill out the form and submit.
	<form method="post" action="viewParameter.jsp">
		Name: <input type="text" name="name" size="10"> <br>
		Address: <input type="text" name="address" size="30"> <br>
		Favorite Animal: <input type="checkbox" name="pet" value="dog">Dog
		<input type="checkbox" name="pet" value="cat">Cat <input
			type="checkbox" name="pet" value="pig">Pig <br> <input
			type="submit" value="Submit">
	</form>
</body>
</html>

<viewParameter.jsp>

<%@ page contentType="text/html; charset=utf-8"%>
<%@ page import="java.util.*"%>

<%
	request.setCharacterEncoding("utf-8");
%>

<html>
<head>
<title>Printing out request method</title>
</head>
<body>

	<b>request.getParameter() Method</b>
	<br> name Parameter =
	<%=request.getParameter("name")%>
	<br> address Parameter =
	<%=request.getParameter("address")%>
	<p>

		<b>request.getParameterValues() Method</b><br>
		<%
			String[] values = request.getParameterValues("pet");
		if (values != null) {
			for (int i = 0; i < values.length; i++) {
		%>
		<%=values[i]%>
		<%
			}
		}
		%>
	
	<p>

		<b>request.getParameterNames() Method</b><br>
		<%
			Enumeration num = request.getParameterNames();
		while (num.hasMoreElements()) {
			String name = (String) num.nextElement();
		%>
		<%=name%>
		<%
			}
		%>
	
	<p>

		<b>request.getParameterMap() Method</b><br>
		<%
			Map parameterMap = request.getParameterMap();
		String[] nameParam = (String[]) parameterMap.get("name");
		if (nameParam != null) {
		%>
		name =
		<%=nameParam[0]%>
		<%
			}
		%>
	
</body>
</html>

 

I would be happy to receive your comment. Please comment below:)

'Java > JSP' 카테고리의 다른 글

JSP) Implicit Objects 3 - Out Object  (0) 2022.08.27
HTML / JSP / Javascript) Implicit Objects 2 - Response Object and Page Link  (0) 2022.08.26
JSP) Scopes - Basics / Application  (0) 2022.08.26
JSP) Comment  (0) 2022.08.26
JSP) Importing / Classes  (0) 2022.08.25

+ Recent posts