jsp2

2024. 1. 4. 21:19Daily Codig Reminder

login

<%@ 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>
<% 
request.setCharacterEncoding("utf-8");
String userid = request.getParameter("userid");
String passwd= request.getParameter("passwd");
out.print(userid+"<br>");
out.print(passwd+"<br>");
%>

사용자  id: <%=userid %><br>
패스워드: <%=passwd %><br>
</body>
</html>

 

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>로그인화면</title>
</head>
<body>
<h1>로그인화면</h1>
<form action="login.jsp"  method="post">
아이디:<input type="text" name="userid"><br>
비밀번호:<input type="text" name="passwd"><br>
<input type="submit" value="로그인">
</form>
</body>
</html>

 

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>로그인화면</title>
</head>
<body>
<h1>로그인화면</h1>
<form action="login.jsp"  method="post">
아이디:<input type="text" name="userid"><br>
비밀번호:<input type="text" name="passwd"><br>
<input type="submit" value="로그인">
</form>
</body>
</html>

taglib

<%@ 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>
<style type="text/css">
h1{color: red;}
</style>
</head>
<body>
<%= request.getContextPath() %><br>
<a href="include/top.jsp">top.jsp상대주소</a><br>
<a href="/include/top.jsp">top.jsp 안됨 (root)</a><br>
<a href="/07_JSP2_include_action/include/top.jsp">top.jsp절대주소</a><br>
<a href="<%= request.getContextPath() %>/include/top.jsp">top.jsp(request)</a><br>
<a href="test.jsp">test.jsp</a><br>


<% request.setAttribute("xxx", "홍길동");
String userid=request.getParameter("userid");
String name="aaa";
%>
first.jsp<br>
userid= <%=userid %><br>
아래화면은 top.jsp를 jsp:include 시킨 내용입니다.
<hr>
<jsp:include page="include/top.jsp" flush="true"/>
<hr>
다시 first.jsp 내용입니다
</body>
</html>

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%! int num=10; %>
<h1>top.jsp</h1>
<% 
String mesg =(String)request.getAttribute("second"); 
String userid=request.getParameter("userid");

%>
<h1>
안녕하세요? mesg===<%=mesg%> <br>
userid====<%=userid%><br>
</h1>
<a href="../test.jsp">1)test.jsp</a><br>
<a href="test.jsp">2) test.jsp</a><br>
<a href="<%= request.getContextPath() %>/test.jsp">3) test.jsp</a><br>
numm===== <%=num%>

 

<%@ 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>
<%
request.setAttribute("xxx", "second");
%>

second.jsp
아래는 top을 jsp:include 시킨 내용입니다.
<br>
<hr>
<jsp:include page="include/top.jsp" flush="true"></jsp:include>
<hr>
<br>

다시 second.jsp입니다.
</body>
</html>

<%@ 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>
test.jsp
</body>
</html>

jsp요청위임

1)기존처리방식

사용자 testservlet

⇒ testservlet.doget(request, response)

요청데이터 파싱, 응답처리까지 모두 처리

 

2,)응답처리 위임

1. response.sendRedirect

사용자 testservlet

⇒ testServlet.doGet(reauest, response)

요청데이터 파싱, 데이터 처리 응답처리 코드 없이

응답처리를 다른 servlet , jsp에 위임

2. RequestDispatcher.forward 반식

RequestDispather dis = request.getRequestDispather(”hello.jsp”);

dis.forward(request, response);

 

package com.controller;

import java.io.IOException;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet("/TestController")
public class TestController extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public TestController() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		System.out.println("TestController.doGet");
		request.setAttribute("request", "홍길동");
		
		HttpSession session = request.getSession();
		session.setAttribute("session", "이순신");
		
		ServletContext ctx = getServletContext();
		ctx.setAttribute("application", "유관순");
		
//		response.sendRedirect("test.jsp");
//		response.sendRedirect("TestController2");
		response.sendRedirect("hello.jsp");
	
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}

package com.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet("/TestController2")
public class TestController2 extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public TestController2() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("TestController2.doGet");
		
		String v1 = (String)request.getAttribute("request");
		
		HttpSession session = request.getSession();
		String v2 = (String)session.getAttribute("session");
		
		ServletContext ctx = getServletContext();
		String v3 = (String)ctx.getAttribute("application");
		
		System.out.println(v1+"\t"+v2+"\t"+v3);
		
		
		response.setContentType("text/html;charset=utf-8");
		
		PrintWriter out = response.getWriter();
		
		out.print("<html><body>");
		out.print("<h1>TestController2</h1>");
		out.print("request : "+v1+"<br>");
		out.print("session : "+v2+"<br>");
		out.print("application : "+v3+"<br>");
		out.print("</body></html>");
		
		
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}

 

 

<%@ 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>
<h1>hello.jsp</h1>
<%

String x= (String)request.getAttribute("request");
String x2 =(String)session.getAttribute("session");

//request는 전송이 안된다
String x3 =(String)application.getAttribute("application");


%>
1>>>><%= x %><br>
2>>>><%= x2 %><br>
3>>>><%= x3 %><br>
</body>
</html>

package com.controller;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.dto.MemberDTO;

@WebServlet("/TestController")
public class TestController extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public TestController() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		System.out.println("TestController.doGet");
		MemberDTO dto = new MemberDTO("BBB", 10);
		request.setAttribute("request", "홍길동");
		request.setAttribute("request2", "aaa");
		request.setAttribute("request3", dto);
		
		HttpSession session = request.getSession();
		session.setAttribute("session", "이순신");
		
		ServletContext ctx = getServletContext();
		ctx.setAttribute("application", "유관순");
		
		RequestDispatcher dis = request.getRequestDispatcher("hello.jsp");
		dis.forward(request, response);
		
//		response.sendRedirect("test.jsp");
	//	response.sendRedirect("TestController2");
//		response.sendRedirect("hello.jsp");
	
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
package com.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet("/TestController2")
public class TestController2 extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public TestController2() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("TestController2.doGet");
		
		String v1 = (String)request.getAttribute("request");
		String vv = (String)request.getAttribute("request2");
		
		HttpSession session = request.getSession();
		String v2 = (String)session.getAttribute("session");
		
		ServletContext ctx = getServletContext();
		String v3 = (String)ctx.getAttribute("application");
		
		System.out.println(v1+"\t"+vv+"\t"+v2+"\t"+v3);
		
		
		response.setContentType("text/html;charset=utf-8");
		
		PrintWriter out = response.getWriter();
		
		out.print("<html><body>");
		out.print("<h1>TestController2</h1>");
		out.print("request : "+v1+"<br>");
		out.print("session : "+v2+"<br>");
		out.print("application : "+v3+"<br>");
		out.print("</body></html>");
		
		
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ page import="com.dto.MemberDTO" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
String x= (String)request.getAttribute("request");
String xx= (String)request.getAttribute("request2");
MemberDTO dto= (MemberDTO)request.getAttribute("request3");
String x2 =(String)session.getAttribute("session");

//request는 전송이 안된다
String x3 =(String)application.getAttribute("application");


%>
1>>>><%= x %><br>
11>>>><%= xx %><br>
2>>>><%= x2 %><br>
3>>>><%= x3 %><br>
4>>>><%= dto.getName() %> : <%= dto.getAge() %><br>

</body>
</html>

'Daily Codig Reminder' 카테고리의 다른 글

jquery  (0) 2024.01.11
mvc  (0) 2024.01.11
5장- jsp  (0) 2024.01.04
servlet 2 + my batis  (1) 2024.01.02
servlet  (0) 2023.12.31