본문 바로가기

Java/java 기초

jsp 내장객체

index.jsp

<%@page import="sample02.Human"%>
<%@page import="java.net.URLEncoder"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

response(응답): sendRedirect -> HttpServletResponse
<%--
request.setCharacterEncoding("utf-8");
String name = "홍길동";
name = URLEncoder.encode(name);
response.sendRedirect("NewFile.jsp?name="+name);
--%>
<%--
String name = "일지매";

request.setAttribute("name", name);
// pageContext : 내장객체
pageContext.forward("NewFile.jsp");
--%>

<%--
int num = 1;
String name = "성춘향";
Human human = new Human(num, name);
request.setAttribute("human", human);	// 짐싸
pageContext.forward("NewFile1.jsp");	// 잘가짐
--%>
<%
int num = 2;
String name = "정수동";

Human human = new Human(num, name);

//많이 사용할 수록 무거워진다
session.setAttribute("hman", human);
response.sendRedirect("NewFile2.jsp");

%>


</body>
</html>

 

NewFile.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%
    request.setCharacterEncoding("utf-8");
    //String name = request.getParameter("name");
    
    String name = (String)request.getAttribute("name");
    %>
    
    
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h3>NewFile.jsp</h3>

<input type="text" value="<%=name %>"><br>
<p><%=name %></p>

</body>
</html>

NewFile1.jsp

<%@page import="sample02.Human"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
	Object obj = request.getAttribute("human");
	Human human = (Human)obj;

%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

<h3>NewFile1.jsp</h3>

num:<input type="text" value="<%= human.getNum()%>">
<br>
name:<p><%=human.getName() %></p>

</body>
</html>

NewFile2.jsp

<%@page import="sample02.Human"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%
    	Human human = (Human)session.getAttribute("hman");
    
    int number = human.getNum();
    String name = human.getName();
    
    session.removeAttribute("hman");
    %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

<p>number:<%=number %></p>
name:<input type="text" value="<%=name%>">


</body>
</html>

'Java > java 기초' 카테고리의 다른 글

Core tag  (0) 2020.01.23
el tag 정리  (0) 2020.01.23
jsp 기초  (0) 2020.01.20
Servlet 기초 응용  (0) 2020.01.17
Servlet session 넘기기및 삭제  (0) 2020.01.17