memberform.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Sign Up</title>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="http://dmaps.daum.net/map_js_init/postcode.v2.js"></script>
<script>
	function openDaumPostcode() {
		new daum.Postcode({
			oncomplete : function(data) {
				document.getElementById('post').value = data.zonecode;
				document.getElementById('address').value = data.address;				
			}
		}).open();
	}
</script>

<script src="member.js"></script>

</head>
<body>

<form method="post" action="member.jsp">
<table border=1 width=600 align=center>
	<caption>Sign Up</caption>
	<tr><th>ID</th>
		<td><input type=text autofocus="autofocus" id="id" name="id">
			<input type="button" value="IDcheck" id="idcheck">
		</td>
	</tr>
	<tr><th>Password</th>
		<td><input type=password id="passwd" name="passwd"></td>
	</tr>
	<tr><th>Name</th>
		<td><input type=text id="name" name="name"></td>
	</tr>
	<tr><th>ID No.</th>
		<td><input type=text size=6 maxlength="6" id="jumin1" name="jumin1">-
		    <input type=text size=7 maxlength="7" id="jumin2" name="jumin2">
		</td>
	</tr>
	<tr><th>EMail</th>
		<td><input type=text size=10 id="mailid" name="mailid">@
			<input type=text size=10 id="domain" name="domain">
			<select id="email">
				<option value="">Email</option>
				<option value="naver.com">Naver</option>
				<option value="daum.net">Daum</option>
				<option value="nate.com">Nate</option>
				<option value="gmail.com">Google</option>
			</select>
		</td>
	</tr>
	<tr><th>Telephone</th>
		<td><input type=text size=4 maxlength="4" id="tel1" name="tel1">-
			<input type=text size=4 maxlength="4" id="tel2" name="tel2">-
			<input type=text size=4 maxlength="4" id="tel3" name="tel3">
		</td>
	</tr>
	<tr><th>Mobile</th>
		<td><select id="phone1" name="phone1">
				<option value="">Select</option>
				<option value="010">010</option>
				<option value="011">011</option>
				<option value="016">016</option>
				<option value="018">018</option>
				<option value="019">019</option>
			</select>-
			<input type=text size=4 maxlength="4" id="phone2" name="phone2">-
			<input type=text size=4 maxlength="4" id="phone3" name="phone3">
		</td>
	</tr>
	<tr><th>Postcode</th>
		<td><input type=text size=5 maxlength="5" id="post" name="post">
			<input type="button" value="Search" onClick="openDaumPostcode()">
		</td>
	</tr>
	<tr><th>Address</th>
		<td><input type=text size=70 id="address" name="address"></td>
	</tr>
	<tr><th>Sex</th>
		<td><input type=radio id="male" name="gender" value="Male">Male
			<input type=radio id="female" name="gender" value="Female">Female
		</td>
	</tr>
	<tr><th>Hobby</th>
		<td><input type=checkbox id="h1" name="hobby" value="Studying">Studying
			<input type=checkbox id="h2" name="hobby" value="Playing games">Playing games
			<input type=checkbox id="h3" name="hobby" value="Hiking">Hiking
			<input type=checkbox id="h4" name="hobby" value="Fishing">Fishing
			<input type=checkbox id="h5" name="hobby" value="Shopping">Shopping
		</td>
	</tr>
	<tr><th>About me</th>
		<td><textarea rows="5" cols="50" id="intro" name="intro"
			  placeholder="Please write up to 100 characters."></textarea></td>
	</tr>
	<tr><td colspan=2 align=center>
			<input type=submit value="Join">
			<input type=reset value="Cancel">
		</td>
	</tr>
</table>
</form>



</body>
</html>

member.js

$(document).ready(function(){
	
		// IDCheck
		$("#idcheck").click(function(){
			if($("#id").val()==""){
				alert("Insert your ID.");
				$("#id").focus();
				return false;
			}else{
				var ref="idcheck.jsp?id="+$("#id").val();
				window.open(ref,"idcheck","width=250, height=150")
			}
		});	
		
		// Focus
		$("#jumin1").keyup(function(){
			
			if($("#jumin1").val().length == 6)
				$("#jumin2").focus();
		});
		
		
		// Domain
		$("#email").change(function(){
			if($("#email").val() == ""){		
				$("#domain").attr("readOnly", false);		
				$("#domain").val("").focus();
			}else{								
				$("#domain").val($("#email").val());
				$("#domain").attr("readOnly", "readOnly");
			}			
		});			
		
		// Validation Check
		$("form").submit(function(){
			
			if($("#id").val() == ""){
				alert("Insert your ID.");
				$("#id").focus();
				return false;
			}
			if($("#passwd").val() == ""){
				alert("Insert your password.");
				$("#passwd").focus();
				return false;
			}
			if($("#name").val() == ""){
				alert("Insert your name.");
				$("#name").focus();
				return false;
			}
			if($("#jumin1").val() == ""){
				alert("Insert your ID number1.");
				$("#jumin1").focus();
				return false;
			}
			if($("#jumin1").val().length != 6){
				alert("Insert your ID number1.");
				$("#jumin1").val("").focus();
				return false;
			}
			// isNaN() 
			if(isNaN($("#jumin1").val())){
				alert("Insert numbers only.");
				$("#jumin1").val("").focus();
				return false;
			}
			if($("#jumin2").val() == ""){
				alert("Insert your ID number2.");
				$("#jumin2").focus();
				return false;
			}
			if($("#jumin2").val().length != 7){
				alert("Insert your ID number1.");
				$("#jumin2").val("").focus();
				return false;
			}
			// isNaN() 
			if(isNaN($("#jumin2").val())){
				alert("Insert numbers only.");
				$("#jumin2").val("").focus();
				return false;
			}
			if($("#mailid").val() == ""){
				alert("Insert your email address.");
				$("#mailid").focus();
				return false;
			}
			if($("#domain").val() == ""){
				alert("Insert the domain of your email.");
				$("#domain").focus();
				return false;
			}
			if($("#tel1").val() == ""){
				alert("Insert your tel1.");
				$("#tel1").focus();
				return false;
			}
			if(isNaN($("#tel1").val())){
				alert("Insert numbers only.");
				$("#tel1").val("").focus();
				return false;
			}
			if($("#tel2").val() == ""){
				alert("Insert your tel2.");
				$("#tel2").focus();
				return false;
			}
			if(isNaN($("#tel2").val())){
				alert("Insert numbers only.");
				$("#tel2").val("").focus();
				return false;
			}
			if($("#tel3").val() == ""){
				alert("Insert your tel3.");
				$("#tel3").focus();
				return false;
			}
			if(isNaN($("#tel3").val())){
				alert("Insert numbers only.");
				$("#tel3").val("").focus();
				return false;
			}
			if($("#phone1").val() == ""){
				alert("Select your mobile number1.");
				return false;
			}
			if($("#phone2").val() == ""){
				alert("Insert your mobile number2.");
				$("#phone2").focus();
				return false;
			}
			if(isNaN($("#phone2").val())){
				alert("Insert numbers only.");
				$("#phone2").val("").focus();
				return false;
			}
			if($("#phone3").val() == ""){
				alert("Insert your mobile number3.");
				$("#phone3").focus();
				return false;
			}
			if(isNaN($("#phone3").val())){
				alert("Insert numbers only.");
				$("#phone3").val("").focus();
				return false;
			}
			if($("#post").val() == ""){
				alert("Insert your postcode.");
				$("#post").focus();
				return false;
			}
			if($("#address").val() == ""){
				alert("Insert your address.");
				$("#address").focus();
				return false;
			}
			if($("#male").is(":checked") == false &&
			   $("#female").is(":checked") == false ){
				alert("Secelte your sex.")
				return false;
			}
			
			if($("input:checkbox[name='hobby']:checked").length < 2){
				alert("Select more than one hobby.")
				return false;
			}
			
			if($("#intro").val() == ""){
				alert("Write About me section.");
				$("#intro").focus();
				return false;
			}
			
			if($("#intro").val().length > 100){
				alert("About me section has to be up to 200 characters.");
				$("#intro").focus();
				return false;
			}
			
		});		
		
	});

member.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%
request.setCharacterEncoding("utf-8");

String id = request.getParameter("id");
String passwd = request.getParameter("passwd");
String name = request.getParameter("name");
String jumin1 = request.getParameter("jumin1");
String jumin2 = request.getParameter("jumin2");
String mailid = request.getParameter("mailid");
String domain = request.getParameter("domain");
String tel1 = request.getParameter("tel1");
String tel2 = request.getParameter("tel2");
String tel3 = request.getParameter("tel3");
String phone1 = request.getParameter("phone1");
String phone2 = request.getParameter("phone2");
String phone3 = request.getParameter("phone3");
String post = request.getParameter("post");
String address = request.getParameter("address");
String gender = request.getParameter("gender");

String[] hobby = request.getParameterValues("hobby");
String h = ""; 
for(String hy : hobby)
	h += hy + "-";

String intro = request.getParameter("intro");
String introduce = request.getParameter("intro").replace("\n", "<br>");

%>

ID : <%=id%> <br>
Password : <%=passwd%> <br>
Name : <%=name%> <br>
ID No. : <%=jumin1%>-<%=jumin2%> <br>
Email : <%=mailid%>@<%=domain%> <br>
Telephone : <%=tel1%>-<%=tel2%>-<%=tel3%> <br>
Mobile : <%=phone1%>-<%=phone2%>-<%=phone3%> <br>
Postcode : <%=post%> <br>
Address : <%=address%> <br>
Sex : <%=gender %> <br>
Hobby : <%=h %> <br>
About me1 : <pre><%=intro%></pre> <br>
About me2 : <br>
<%=introduce%> <br>
Postcode : <%=post%> <br>
ID : <%=id%> <br>

idcheck.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

 ID : <%=request.getParameter("id") %>

boardform.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Bulletin Board</title>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="board.js"></script>

</head>
<body>

<form method="post" action="board.jsp">
<table border=1 width=600 align=center>
	<caption>Bulletin Board</caption>
	<tr><th>Writer</th>  <!-- th : align center , bold  -->
		<td><input type=text size=30 id="writer" name="writer" 
							         placeholder="Writer"></td>
	</tr>
	<tr><th>Password</th>
		<td><input type=password size=30 id="passwd" name="passwd" placeholder="2~8 chracters"></td>
	</tr>
	<tr><th>Title</th>
		<td><input type=text size=60 id="subject" name="subject"></td>
	</tr>
	<tr><th>Content</th>
		<td><textarea rows="5" cols="50" id="content" name="content" placeholder="Write up to 200 characters."></textarea></td>
	</tr>
	<tr><th>Attach File</th>
		<td><input type=file></td>
	</tr>
	<tr><td colspan=2 align=center>
			<input type=submit value="Post">	 
			<input type=reset value="Cancel">     
		</td>
	</tr>
</table>
</form>

</body>
</html>

board.js

$(function(){
		$("form").submit(function(){
			if($.trim($("#writer").val()) == ""){
				alert("Insert writer name.");
				$("#writer").focus();
				return false;
			}
			if($.trim($("#passwd").val()) == ""){
				alert("Insert password.");
				$("#passwd").focus();
				return false;
			}
			if($("#passwd").val().length <2 || 
			   $("#passwd").val().length >8	){
				alert("Password has to be 2 ~ 8 characters.");
				$("#passwd").val("").focus();
				return false;
			}
			if($.trim($("#subject").val()) == ""){
				alert("Insert title.");
				$("#subject").focus();
				return false;
			}
			if($.trim($("#content").val()) == ""){
				alert("Insert content");
				$("#content").focus();
				return false;
			}
			if($.trim($("#content").val()).length > 200){
				alert("Content has to be up to 200 characters.");
				$("#content").focus();
				return false;
			}
		});		
	});

 

board.jsp(Option1)

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%
request.setCharacterEncoding("utf-8");

String writer = request.getParameter("writer");
String passwd = request.getParameter("passwd");
String subject = request.getParameter("subject");
String content = request.getParameter("content");
%>

Writer : <%=writer%> <br>
Password : <%=passwd%> <br>
Subject : <%=subject%> <br>
Content : <pre><%=content%></pre> <br>

The <pre></pre> tag helps to display the text exactly as written in the HTML source code.

board.jsp(Option2)

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%
request.setCharacterEncoding("utf-8");

String writer = request.getParameter("writer");
String passwd = request.getParameter("passwd");
String subject = request.getParameter("subject");
String content = request.getParameter("content");

String contents = 
	request.getParameter("content").replace("\n","<br>");
// <br> will be replaced instead of "\n"
%>

Writer : <%=writer%> <br>
Password : <%=passwd%> <br>
Subject : <%=subject%> <br>
Content1 : <pre><%=content%></pre> <br>
Content2 : <%=contents%>

'Codes & Projects' 카테고리의 다른 글

JSP) Log in / Shopping cart  (0) 2022.08.29
HTML / Javascript / JSP ) Sign up Form2  (0) 2022.08.28
Javascript/ JSP/ HTML ) Log in form  (0) 2022.08.27
Java) Finding Maximum value  (0) 2022.08.25
Java) Math.random()  (0) 2022.08.25

<HTML>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Log in</title>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
	/* $(document).ready(function(){
	$("form").submit(function(){
		if($.trim($("#id").val()) == ""){
			alert("Insert your ID.");
			$("#id").focus();
			return false;
		}			
		if($.trim($("#passwd").val()) == ""){
			alert("Insert your password.");
			$("#passwd").focus();
			return false;
		}			
	});		
	});	  */
</script>

<script src="login.js"></script>

</head>
<body>

	<form method="post" action="login.jsp">
		<table border=1 width=350 align=center>
			<caption>Log in</caption>
			<tr>
				<th>ID</th>
				<td><input type=text size=30 autofocus="autofocus" id="id"
					name="id"></td>
			</tr>
			<tr>
				<th>Password</th>
				<td><input type=password size=30 id="passwd" name="passwd"></td>
			</tr>
			<tr>
				<td colspan=2 align=center><input type=submit value="Log in">
					<input type=reset value="Cancel"></td>
			</tr>
		</table>
	</form>

</body>
</html>

<Javascript>

$(document).ready(function(){
		$("form").submit(function(){
			if($.trim($("#id").val()) == ""){
				alert("Insert your ID.");
				$("#id").focus();
				return false;
			}			
			if($.trim($("#passwd").val()) == ""){
				alert("Insert your password.");
				$("#passwd").focus();
				return false;
			}			
		});		
});

<JSP>

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>

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

String id = request.getParameter("id");
String passwd = request.getParameter("passwd");
%>

ID :
<%=id%>
<br>
Password :
<%=passwd%>
<br>

'Codes & Projects' 카테고리의 다른 글

HTML / Javascript / JSP ) Sign up Form2  (0) 2022.08.28
HTML / Javascript /JSP ) Bulletin Board  (0) 2022.08.28
Java) Finding Maximum value  (0) 2022.08.25
Java) Math.random()  (0) 2022.08.25
jQuery/ JSP) Sign up Form  (0) 2022.08.24

본 내용은 아래 링크에 나온 Request Object Example1의 한글 인코딩과 관련된 포스팅입니다. 

This post is related to the Korean encoding of Request Object Sample1 shown in the link below.

https://agilemeadow.tistory.com/manage/newpost/?type=post&returnURL=%2Fmanage%2Fposts%2F# 

 

TISTORY

나를 표현하는 블로그를 만들어보세요.

www.tistory.com

<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>

인코딩은 항상 위에서 먼저 해야 합니다. 그렇지 않으면 한글 값이 깨집니다.

Post 방식으로 값을 전달 할 때는 Tomcat이 자동으로 인코딩하지 않으므로 인코딩을 먼저 시킨다음 값을 받아야 합니다. 

대부분의 전송방식이 Post 방식이기 때문에 한글 인코딩은 항상 빠지지 않는 중요한 문제입니다. 

Encoding should always be done first above. Otherwise, the Korean value is broken.
Tomcat does not automatically encode the value when forwarding it in the Post method, so you must encode it first and then receive it. 
Since most transmission methods are Post, encoding Korean is always an important issue.

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

	
<%	
// 한글 인코딩
// 1. 폼 파일에서 한글값이 get방식으로 전송될 때는 tomcat이 자동으로 utf-8로 인코딩을 시켜준다. 
// 2. 폼 파일에서 한글값이 post 방식으로 전송될 때는 tomcat이 자동으로 인코딩을 시켜주지 않기 때문에 
//    아래의 코드로 직접 인코딩해야 한다. 
// 폼파일에서 한글값이 post방식으로 전송될때 utf-8로 인코딩을 시켜주는 역할

// Korean encoding
// 1. When the Korean value is transmitted in the form file by the get method, tomcat automatically encodes it to utf-8. 
// 2. Tomcat does not automatically encode the Korean value in the form file when it is sent in the post method 
//    It shall be encoded directly into the code below. 
// The role of encoding the Korean values in the form file to utf-8 when they are transmitted by post.

	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>

get방식과 post 방식의 차이에 대해 궁금하시다면 이 상단의 링크를 참조해주세요. 

If you are curious about the difference between get method and post method, please refer to the link above/ or refer to my last post, <JSP) Implicit Objects 1>.

 

Please comment below if you have any issues or questions about this post. 

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

JSP) Session  (0) 2022.08.29
JSP) Cookies  (0) 2022.08.28
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) Implicit Objects 1 - Request Object  (0) 2022.08.26

An out object is an output stream that transmits the results generated by the JSP page to the web browser, and all information that the JSP page sends to the web browser is transmitted through the out object.  All information includes both script and non-script elements such as HTML and general text. Here are some examples of the out object.

 

You can check the size and the use of Buffer. 

outEx.jsp

<%@ page contentType="text/html;charset=euc-kr" buffer="5kb"%>

<%
	int totalBuffer = out.getBufferSize();
	int remainBuffer = out.getRemaining();
	int useBuffer = totalBuffer - remainBuffer;
%>

<h1>Out Example</h1>
<b>Buffer status on current page</b><p>

Total Buffer Size : <%=totalBuffer%>byte<p>
Current Use of Buffer : <%=useBuffer%>byte<p>
Remain Buffer Size : <%=remainBuffer%>byte<p>

useOut.jsp

<%@ page contentType = "text/html; charset=euc-kr" %>

<html>
<head><title>out Object2</title></head>
<body>

<%
    out.println("What's up?");
%>
<br>

This is the result of
<%
    out.println("the basic out object.");
%>

</body>
</html>

 

 

println in JSP does not change the line.

To change the line, you need to put the <br> tag like this :

<%@ page contentType = "text/html; charset=euc-kr" %>

<html>
<head><title>out Object2</title></head>
<body>

<%
    out.println("What's up?<br>");
    out.println("Let's change the line.<br>");
%>
<br>

This is the result of
<%
    out.println("<br>the basic out object.");
%>

</body>
</html>

<br> tags are used.

 

There are two data types in Java : Primitive and Reference.

They are the basic of basic in Java and very important to know to jump to the next step, so let's make them familiar!

Source : Javarevisted

 

Primitive Type

Type Size Description
byte 1 byte Stores whole numbers from -128 to 127
short 2 bytes Stores whole numbers from -32,768 to 32,767
int 4 bytes Stores whole numbers from -2,147,483,648 to 2,147,483,647
long 8 bytes Stores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
float 4 bytes Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits
double 8 bytes Stores fractional numbers. Sufficient for storing 15 decimal digits
boolean 1 bit Stores true or false values
char 2 bytes Stores a single character/letter or ASCII values

Reference Type

Type Description
Class Describes the content of the object
Array Stores the elements of the same type
Interface A collection of abstract methods

The difference between Primitive and Reference Type is that

Source : Javarevisited

Variables 

Variable is a storage space for storing data on memor.

package p2022_06_21;

import java.util.ArrayList;

public class Variable {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		// 1. Integer Type
		byte b1 = 10; 			
		
		short s = 100; 
		
		int i = 1000; 
		long l = 100000L;  
		
		System.out.println("b1="+b1);
		System.out.println("s="+s);
		System.out.println("i="+i);
		System.out.println("l="+l);
		
		//2. Rational Type
		float ft1 = 3.14f;  
		// float ft1 = (float)3.14; 
		float ft2 = 3.14F; 
		double d = 42.195; 
		
		System.out.println("ft1="+ft1);
		System.out.println("ft2="+ft2);
		System.out.println("d="+d);
		
		
		System.out.printf("%.1f\n", d); 
		System.out.printf("%.2f", d); 
		System.out.printf("%.2f\n",d); 
		
		
		//3. Character Type
		char c1 = 'A';
		char c2 = '安'; 
		
		System.out.println("c1="+c1);
		System.out.println("c2="+c2);
		
		//4. Boolean Type
		boolean bn1 = true; 
		boolean bn2 = false;
		System.out.println("bn1="+bn1);
		System.out.println("bn2="+bn2);

		//Reference Type : Class
		String s1 = "Java";
		String s2 = new String("Java");
		
		System.out.println("s1="+s1);
		System.out.println("s2="+s2);
		if(s1 == s2){
			System.out.println("Same Address");
		} else {
			System.out.println("Different Address");
		}
		if(s1.equals(s2)){
			System.out.println("Same Value");
		} else {
			System.out.println("Different Value");
		}	
		
		//Reference variable : 배열- 동일한 자료형의 데이터를 저장하는 정적인 자료구조 
		int[] score = {80, 90, 100};
		
		for(int j=0; j<score.length; j++) {
			System.out.println(score[j]+"\t");
		}
		
		System.out.println();
			
		//Reference Type : Interface(List)
	
	  //List list = new List();  //Error 
		ArrayList list = new ArrayList(); //Upcasting
		list.add(30);
		list.add(3.14);
		list.add('j');
		list.add(true);
		list.add("Java");
		
		for(int k=0; k<list.size(); k++) {
		System.out.print(list.get(k)+"\t"); //\t는 insert a new tap 
		}

}

}

'Java' 카테고리의 다른 글

Java) Array  (0) 2022.09.04
Java) .length vs length()  (0) 2022.09.02
Java) Conditional statements and loops  (0) 2022.08.30
Java) Operators  (0) 2022.08.26
Java (Must know)  (0) 2022.08.25

Response Object is the object of  HttpServletResponse class associated with the response to the client. It is different from the request object since the request is the object of HttpServletRequest class associated with the request.

 

We will cover three ways to link pages with HTML, Javascript, and JSP. It is the same function, but each language has different codes to link the page. first.jsp is the file that we will link in each files. 

 

first.jsp

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

<%

String str = request.getParameter("name");

%>

name : <%=str%>

 

meta.html

The good thing when you use the meta tag in HTML is that you can set the time.

<html>
	<head>

	<!-- mata tag -->
	<meta http-equiv="refresh" content="0;
		url=first.jsp?name=test">

	</head>
	<body>
	</body>
</html>

The url will link to the first.jsp file. This is a get method. If you are using a form, you can choose either get method or post method, but in this case, you cannot. 

 

With Javascript, you can use the alert() and location property.

location.html

<html>
	<head>
	<meta charset="utf-8">
	</head>
	<body>

	<script language="javascript">
		alert("Page link");
		location.href='first.jsp?name=test';
	</script>

	</body>
</html>

With JSP, you can use a response object. 

responseEx.jsp

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

<h1>Response Example</h1>
This page is from responseEx.jsp file.


<%	//Page link from JSP
     response.sendRedirect("first.jsp?name=test");
%>

 

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

JSP) Encoding Korean 한글 인코딩  (0) 2022.08.27
JSP) Implicit Objects 3 - Out Object  (0) 2022.08.27
JSP) Implicit Objects 1 - Request Object  (0) 2022.08.26
JSP) Scopes - Basics / Application  (0) 2022.08.26
JSP) Comment  (0) 2022.08.26

 

Operators are used to performing operations on variables and values.

There are six types of operators, and it will be good for you once you understand how they work in Java because it will be very similar to other languages. 

  • Arithmetic Operators
    • +, -, *, /, % (remaining)
  • Comparison operators (=relational operator)
    • >=, <=, == (same), != (same)
    • ex) if(a == b){} // if a and b are equal
    • if(a!=b){} // if a and b are not equal
  • Condition operators = (conditional expression)
    • ? Value1: Value2;
      If the conditional expression is true, assign the value 1 to the variable
      If the conditional expression is false, assign the value 2 to the variable.
  • Logical operators
    • ||, &&,!
  • Extended substitution operators
    • +=, -=, *=, /=, %=
    • ex) a+=b; // a = a + b;
      a-=b; // a = a - b;
      a*=b; // a = a * b;
      a/=b; // a = a / b;
      a%=b; // a = a % b;
  • Increase/decrease operators
    • ++ Increasing by 1 ++a (pre-processing) // a=a+1;
      a++ (following) // a=a+1;
    • -- Decrease by one --a (pre-processing) // a=a-1;
      a--(following) // a=a-1;

Here are some examples of using the operators in Java. 

 

Arithmetic Operators

public class Oper01 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int a = 10, b = 3, c;
		
		c = a + b;
		System.out.println("a+b="+c);
		System.out.println("a+b="+(a+b));
		System.out.println("a-b="+(a-b));
		System.out.println("a*b="+(a*b));
		System.out.println("a/b="+(a/b)); 
		System.out.println("a%b="+(a%b)); 
		
        //Character
		String str1 = "Java";
		String str2 = str1 + "Oracle";
		System.out.println("Str1="+str1);
		System.out.println("Str2="+str2);
		
		String str3 = "Python";
		String str4 = "Spring";
		System.out.println(str3 + str4); 
		
		int i = 50; 
		System.out.println(str3 + i); 
		
		String str5 = str3 + 50;
		System.out.println("str5="+str5); 
			
	}

}

Comparison operators (=relational operator)

public class Oper03 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		// Comparison Operator: > , < , >= , <= , == , !=
		
		int num1 = 10; 
		int num2 = 10;
		
		boolean result1 = (num1 == num2);
		boolean result2 = (num1 != num2);
		boolean result3 = (num1 <= num2);
		System.out.println("result1="+result1);
		System.out.println("result2="+result2);
		System.out.println("result3="+result3);
		
		System.out.println(num1 > num2);
		
		char c1 = 'A'; //65
		char c2 = 'B'; //66
		boolean result4 = (c1 < c2);
		System.out.println("result4="+result4);

		// Comparison Operator2
		String str1 = "Java";
		String str2 = "Java";
		String str3 = new String("Java");
		
		// Comparing the addresses
		if(str1 == str2) {
			System.out.println("Same Address");
		}else {
			System.out.println("Different Address");
		}
		
		if(str1 == str3) {
			System.out.println("Same Address");
		}else {
			System.out.println("Different Address");
			
			// Comparing the values
			System.out.println(str1.equals(str2));
			System.out.println(str1.equals(str3));
		}
	}

}

Condition operators = (conditional expression)

import java.util.Scanner;

public class Oper6 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		// Condition Operator
        // Variable = (Conditional statement) ? Value1 : Value2;
        // True -> Value1 / False -> Value2

		// Maximum and minimum values out of the two integers entered on the keyboard.
		
		int n1, n2, max, min; 
		System.out.println("Enter two integers");
		
		Scanner sc = new Scanner(System.in);
		n1 = sc.nextInt();
		n2 = sc.nextInt();
		
		max = (n1 > n2) ? n1 : n2;
		min = (n1 < n2) ? n1 : n2;
		
		System.out.println("max="+max);
		System.out.println("min="+min);
		
	}

}

Logical Operators

 The results are treated as int-type if you perform arithmetic operations on the int-type and int-type variables. If the double and double types are performed, the result is treated as a double type. The results are treated as double types if you perform arithmetic operations of int and double types. 

import java.util.Scanner;

public class Oper07 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		// Write a program that determines whether you pass or fail 5 subjects when you enter them on the keyboard
		// Each subject's class is 40 points, and an average of 60 points or more is required to pass.
		
		int n1, n2, n3, n4, n5, total;
		double avg;
		System.out.println("Enter the scores of 5 subjects.");
		
		Scanner sc = new Scanner(System.in);
		
		n1 = sc.nextInt();
		n2 = sc.nextInt();
		n3 = sc.nextInt();
		n4 = sc.nextInt();
		n5 = sc.nextInt();
		

		total = n1 + n2 + n3 + n4 + n5;
		avg = (double)total / (double)5;
		System.out.println("avg="+avg);
		
		if(n1>=40 && n2>=40 && n3>=40 && n4>=40 && n5>=50 && avg>= 60) {
			System.out.println("Success");
		}else {
			System.out.println("Fail");
		}
		
}
}
public class Oper08 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		boolean b1 = true;
		boolean b2 = false;
		
		System.out.println(!b1);   
		//false !not operator changes to the opposite
		System.out.println(!b2);  
		System.out.println(!true);  
		System.out.println(!false);  
	}

}

Extended substitution operators

public class Oper09 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		int a = 10, b = 3;
		System.out.println(a+=b); 
		// a=a+b
		System.out.println(a-=b); 
		System.out.println(a*=b); 
		System.out.println(a/=b); 
		System.out.println(a%=b); 
	}

}

Increase/decrease operators

public class Oper10 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		int a=10, b=10, c=10, d=10;
		int a1, b1, c1, d1;
		
		a1 = ++a;  //Prefix Operator
		b1 = b++;  //Postfix Operator
		c1 = --c;  //Prefix Operator
		d1 = d--;  //Postfix Operator
		
		System.out.println("a1="+a1+" a="+a);
		System.out.println("b1="+b1+" b="+b);
		System.out.println("c1="+c1+" c="+c);
		System.out.println("d1="+d1+" d="+d);
		
		int a=10, b=10;
		System.out.println("a="+a++); //Postfix Operator : a=10
		System.out.println("a="+a); //a=11
		
		System.out.println("b="+(++b)); //Prefix Operator :b=11
		System.out.println("b"+b); //b=11
	}

}

 

'Java' 카테고리의 다른 글

Java) Array  (0) 2022.09.04
Java) .length vs length()  (0) 2022.09.02
Java) Conditional statements and loops  (0) 2022.08.30
Java) Data Types  (0) 2022.08.27
Java (Must know)  (0) 2022.08.25

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

There are four scopes in JSP. 

Scope Description
Page Stores a value to be shared within one JSP page.
Request Stores values to be shared on all JSP pages used to process a single request.
Session Shares information related to a user.
Application Stores information to be shared with all users.

For your better understanding, please refer to this picture. Page is the narrowest scope, and the application is the widest scope. 

The basic objects related to JSP's scopes are pageContext, request, session, and application.

 

Here are some examples of scopes. With them, you will have a better understanding of which data will be saved in which scope. 

AtrributeTest1_Form.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<html>
<head>
<title>Attribute Test Form</title>
</head>
<body>
<h2>Scopes and properties</h2>
<form action="attributeTest1.jsp" method="post">
<table border="1">
	<tr><td colspan="2">Contents for Application</td></tr>
	<tr>
		<td>Name</td>
		<td><input type="text" name="name"></td>
	</tr>
	<tr>
		<td>ID</td>
		<td><input type="text" name="id"></td>
	</tr>
	<tr>
		<td colspan="2"><input type="submit" value="전송"></td>
	</tr>
</table>
</form>
</body>
</html>

AttributeTest1.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<html>
<head>
<title>Attribute Test</title>
</head>
<body>
<h2>Scopes and properties</h2>
<%
request.setCharacterEncoding("euc-kr");
String name=request.getParameter("name");
String id=request.getParameter("id");
if(name!=null&&id!=null){
	application.setAttribute("name",name);
	application.setAttribute("id",id);
}
%>
<h3>Welcome, <%=name %>.<br><%=name %>'s ID is <%=id %>.</h3>
<form action="attributeTest2.jsp" method="post">
<table border="1">
	<tr><td colspan="2">Contents for Session</td></tr>
	<tr>
		<td>E-mail</td>
		<td><input type="text" name="email"></td>
	</tr>
	<tr>
		<td>Address</td>
		<td><input type="text" name="address"></td>
	</tr>
	<tr>
		<td>전화번호</td>
		<td><input type="text" name="tel"></td>
	</tr>
	<tr>
		<td colspan="2"><input type="submit" value="전송"></td>
	</tr>
</table>
</form>
</body>
</html>

AttributeTest2.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<html>
<head>
<title>Attribute Test</title>
</head>
<body>
<h2>Scopes and properties</h2>
<%
request.setCharacterEncoding("euc-kr");
String email=request.getParameter("email");
String address=request.getParameter("address");
String tel=request.getParameter("tel");
session.setAttribute("email",email);
session.setAttribute("address",address);
session.setAttribute("tel",tel);

String name=(String)application.getAttribute("name");
%>
<h3><%=name %>'s data is successfully saved.</h3>
<a href="attributeTest3.jsp">Click to check</a>
</body>
</html>

 

AttributeTest3.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%@page import="java.util.Enumeration"%>
<html>
<head>
<title>Attribute Test</title>
</head>
<body>
<h2>Scopes and properties</h2>
<table border="1">
	<tr><td colspan="2">Content for Application</td></tr>
	<tr>
		<td>Name</td>
		<td><%=application.getAttribute("name") %></td>
	</tr>
	<tr>
		<td>ID</td>
		<td><%=application.getAttribute("id") %></td>
	</tr>
</table>
<br>
<table border="1">
	<tr><td colspan="2">Content for Session</td></tr>
<%-- <%
Enumeration e=session.getAttributeNames();
while(e.hasMoreElements()){
	String attributeName=(String)e.nextElement();
	String attributeValue=(String)session.getAttribute(attributeName);
	%>
	<tr>
		<td><%=attributeName %></td>
		<td><%=attributeValue %></td>
	</tr>
	<%
}
%> -->

		<tr>
			<td>Email</td>
			<td><%=session.getAttribute("email")%></td>
		</tr>
		<tr>
			<td>Tel</td>
			<td><%=session.getAttribute("tel")%></td>
		</tr>
		<tr>
			<td>Address</td>
			<td><%=session.getAttribute("address")%></td>
		</tr>
</table>
</body>
</html>

Since the Name and the ID are shared by the application object, you can use that data in other scopse too, because the application object is the broadest scope. 

 

To set application Attribute

index.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
My first JSP program <br>

Value shared by application object : 
<%=application.getAttribute("test")%>

</body>
</html>

setApplicationAttribute.jsp

<%@ page contentType = "text/html; charset=euc-kr" %>
<%
   // String name = request.getParameter("name");
   // String value = request.getParameter("value");
    
    String name = "test";
    String value = "1234";
    
    if (name != null && value != null) {
        application.setAttribute(name, value);
      //application.setAttribute("test", "1234");
    }
%>

<html>
<head><title>Set application attribute</title></head>
<body>
<%
    if (name != null && value != null) {
%>
Set attribute of application basic objects:
 <%= name %>  = <%= value %>
<%
    } else {
%>
Application basic objects attribute is not set.
<%
    }
%>
</body>
</html>

These examples are focused on the application scope. If you want to see other examples of the session scope, please refer to this post of mine: 2022.08.29 - [JSP] - JSP) Session

 

JSP) Session

A session is a concept used by servers and clients to maintain a connection state. Usually, if the login is successful, it is connected to the client as a session on the server side. At this time, m..

www.agilemeadow.com

 

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

HTML / JSP / Javascript) Implicit Objects 2 - Response Object and Page Link  (0) 2022.08.26
JSP) Implicit Objects 1 - Request Object  (0) 2022.08.26
JSP) Comment  (0) 2022.08.26
JSP) Importing / Classes  (0) 2022.08.25
JSP Tags  (0) 2022.08.25

+ Recent posts