๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
IT/WEB

[36์ผ์ฐจ] Servlet. jsp

by GWLEE 2022. 8. 10.

๐Ÿ’™2022-08-10๐Ÿ’™

 

ํ—ค๋” ๋ถ€๋ถ„์ด ์žˆ๊ณ 

response๋Š” ์‹ค์ œ html ์†Œ์Šค ์ „๋‹ฌํ•˜๋ ค๋Š” ๋‚ด์šฉ์ด ๋“ค์–ด์žˆ๋‹ค.

์ดˆ์ฐฝ๊ธฐ WWW ๋งต์˜ ๋ชจ์Šต.. ์—ฌ๊ธฐ์„œ ์•Œ ์ˆ˜ ์žˆ๋“ฏ์ด ์˜›๋‚  ๋ถ€ํ„ฐ ๊ฑฐ์Šฌ๋Ÿฌ ์˜ฌ๋ผ๊ฐ€์ž๋ฉด, 

๋ฏธ๋ฆฌ ๋งŒ๋“ค์–ด๋‘” html ๋ฌธ์„œ๋ฅผ ๋ณด์—ฌ์ค€๋‹ค. ์ฒ ์ €ํžˆ staticํ•œ ๋ฆฌ์†Œ์Šค๋งŒ์„ ๋ณด์—ฌ์ค€๋‹ค. ๋ฏธ๋ฆฌ ๋งŒ๋“ค์–ด๋‘”..

๋ธŒ๋ผ์šฐ์ € ํ™”๋ฉด์— ์นธ์—๋‹ค๊ฐ€ ์ž๊ธฐ ํ•™๋ฒˆ์„ ์ง‘์–ด ๋„ฃ๊ณ ์„œ ๋ฒ„ํŠผ์„ ๋ˆŒ๋ €์„ ๋•Œ ๋ณธ์ธ์— ๋Œ€ํ•œ ์ž๋ฃŒ

๋‹ค์ด๋‚˜๋ฏนํ•œ ์ž๋ฃŒ,,, static ๋‹ค์ด๋‚˜๋ฏน ๊ทธ๋ฆผ์ด ์›€์ง์ด๋ƒ ์•ˆ์›€์ง์ด๋ƒ ๊ทธ๊ฒŒ ์•„๋‹ˆ๋ผ staticํ•˜๋ƒ dynamicํ•˜๋ƒ,,,

์‚ฌ์šฉ์ž ์š”์ฒญ์— ๋”ฐ๋ผ์„œ ๊ตฌ๋ถ„ํ•ด์„œ ๋™์ž‘ํ•˜๋Š”๊ฒŒ ํ•„์š”ํ•˜๋‹ค.

์ƒˆ๋กœ์šด ์‹œ๋„ -> ์ž๋ฐ” ๊ธฐ์ค€ ! ์‚ฌ์šฉ์ž๊ฐ€ ์š”์ฒญํ•˜๋Š” ๊ฑฐ์— ๋”ฐ๋ผ์„œ ๋‹ค๋ฅธ ๋™์ž‘์„ ํ•ด์ฃผ๋ ค๋ฉด ์ž๋ฐ”๋กœ ์ง  ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์ด ํ•„์š”ํ•˜๋‹ค.

servelt ์„œ๋ธ”๋ฆฟ 

์„œ๋ธ”๋ฆฟ์ด๋ž€ Dynamic Web Page๋ฅผ ๋งŒ๋“ค ๋•Œ ์‚ฌ์šฉ๋˜๋Š” ์ž๋ฐ” ๊ธฐ๋ฐ˜์˜ ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๊ธฐ์ˆ ์ž…๋‹ˆ๋‹ค

 

์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์„œ๋ฒ„๋‹ค. WAS๋กœ ์ค„์—ฌ์„œ ์“ด๋‹ค. Apache Tomcat ๋ฌด๋ฃŒ ๋ฒ„์ „ web sphere JEWS ๊ตญ์‚ฐ ์‹ ๊ธฐ์ˆ 

 

9.0.65 ๋‹ค์šด๋กœ๋“œ

 

 

์œˆ๋„์šฐ start 

 

other - server

 

doget get๋ฐฉ์‹์œผ๋กœ ํ˜ธ์ถœํ•  ๋•Œ default๋Š” get ๋ฐฉ์‹ 

์–ธ์ œ ์–ด๋–ค ๋ฐฉ์‹ ๋งคํ•‘

 

first๋ผ๋Š” ์ฃผ์†Œ๋กœ ์˜ฌ ๋•Œ ์„œ๋ธ”๋ › ๋งคํ•‘

 

loginServlet 

 

 

login.html

 

 

server ์ผœ์„œ login.html

 

 

 

 

 


 

 

LoginServlet.java



package com.gyuone.test01;

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

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

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("utf-8");
String user_id = request.getParameter("user_id"); /* ์ž…๋ ฅ ์•ˆ์— ์žˆ๋Š” nameparameter ์„œ๋ฒ„๋ž‘ attribute๋ฅผ ๋งž์ถ˜๋‹ค.*/
String user_pw = request.getParameter("user_pw");
System.out.println("์•„์ด๋””: " + user_id);
System.out.println("ํŒจ์Šค์›Œ๋“œ: " + user_pw);

}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
req.setCharacterEncoding("utf-8");
String user_id = req.getParameter("user_id"); /* ์ž…๋ ฅ ์•ˆ์— ์žˆ๋Š” nameparameter ์„œ๋ฒ„๋ž‘ attribute๋ฅผ ๋งž์ถ˜๋‹ค.*/
String user_pw = req.getParameter("user_pw");

resp.setContentType("text/html;charset=utf-8");
PrintWriter out = resp.getWriter();

StringBuffer sb = new StringBuffer();
sb.append("<html>");
sb.append("<body>");
sb.append("์•„์ด๋””");
sb.append(user_id);
sb.append("<br>");
sb.append("๋น„๋ฐ€๋ฒˆํ˜ธ : ");
sb.append(user_pw);
sb.append("</body>");
sb.append("</html>");
out.print(sb.toString());
}
}

 

 


package com.gyuone.test01;

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

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

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		String user_id = request.getParameter("user_id"); /* ์ž…๋ ฅ ์•ˆ์— ์žˆ๋Š” nameparameter ์„œ๋ฒ„๋ž‘ attribute๋ฅผ ๋งž์ถ˜๋‹ค.*/
		String user_pw = request.getParameter("user_pw");
		System.out.println("์•„์ด๋””: " + user_id);
		System.out.println("ํŒจ์Šค์›Œ๋“œ: " + user_pw);
		
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		req.setCharacterEncoding("utf-8");
		String user_id = req.getParameter("user_id"); /* ์ž…๋ ฅ ์•ˆ์— ์žˆ๋Š” nameparameter ์„œ๋ฒ„๋ž‘ attribute๋ฅผ ๋งž์ถ˜๋‹ค.*/
		String user_pw = req.getParameter("user_pw");
		
		resp.setContentType("text/html;charset=utf-8");
		PrintWriter out = resp.getWriter();
		
		StringBuffer sb = new StringBuffer();
		sb.append("<html>");
		sb.append("<body>");
		sb.append("์•„์ด๋”” : ");
		sb.append(user_id);
		sb.append("<br>");
		sb.append("๋น„๋ฐ€๋ฒˆํ˜ธ : ");
		sb.append(user_pw);
		sb.append("</body>");
		sb.append("</html>");	
		out.print(sb.toString());
	}
}


 

 

JSP (Java Server Page)

  • ์ดˆ๊ธฐ ์›น ํ”„๋กœ๊ทธ๋žจ์€ ์„œ๋ธ”๋ฆฟ์„ ์ด์šฉํ•ด์„œ ๊ตฌํ˜„
  • ํ™”๋ฉด์˜ ๊ธฐ๋Šฅ์ด๋‚˜ ๊ตฌ์„ฑ์ด ๋ณต์žกํ•ด์ง์— ๋”ฐ๋ผ ํ”„๋กœ๊ทธ๋ž˜๋จธ๊ฐ€ ์„œ๋ธ”๋ฆฟ์œผ๋กœ ํ™”๋ฉด์„ ๊ตฌํ˜„ํ•˜์ง€ ์•Š๊ณ  ์ฃผ๋กœ ๋””์ž์ด๋„ˆ๊ฐ€ ์ด ์ผ์„ ๋‹ด๋‹น
  • ์„œ๋ธ”๋ฆฟ์˜ ๊ธฐ๋Šฅ ์ค‘ ๋ณ„๋„๋กœ ํ™”๋ฉด ๊ธฐ๋Šฅ์„ ๋””์ž์ด๋„ˆ๊ฐ€ ์ž‘์—…ํ•˜๊ธฐ ์‰ฝ๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด JSP๊ฐ€ ๋“ฑ์žฅ
  • JSP๋Š” ๋””์ž์ด๋„ˆ ์ž…์žฅ์—์„œ ํ™”๋ฉด์˜ ์ˆ˜์›”ํ•œ ๊ธฐ๋Šฅ ๊ตฌํ˜„๊ณผ ๊ฐœ๋ฐœ ํ›„ ํ™”๋ฉด์˜ ํŽธ๋ฆฌํ•œ ์œ ์ง€๊ด€๋ฆฌ๋ฅผ ๋ชฉ์ ์œผ๋กœ ๋„์ž…
  • JSP๋Š” HTML, CSS์™€ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ JSP ์š”์†Œ๋“ค์„ ์‚ฌ์šฉํ•ด ํ™”๋ฉด์„ ๊ตฌํ˜„

E:\source\java\eclipse-workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\work\Catalina\localhost\ServletTest\org\apache\jsp

 


hello2.jsp

 


hello3.jsp

 


	<%@ page language="java" contentType="text/html; charset=UTF-8"
	    import="java.util.*"
	    import="sec02.ex01.*"
	    pageEncoding="UTF-8"
	%>
	<!DOCTYPE html>
	<html>
	<head>
	<style>
	h1 {
	text-align: center;
	}
	</style>
	  <meta charset="UTF-8">
	<title>ํšŒ์› ์ •๋ณด ์ถœ๋ ฅ์ฐฝ</title>
	</head>
	<body>
	<h1>ํšŒ์› ์ •๋ณด ์ถœ๋ ฅ</h1>
	<%
	   request.setCharacterEncoding( "utf-8" );
	   String _name = request.getParameter("name");
	   CustomerVO customerVO = new CustomerVO();
	   customerVO.setCustName(_name);
	   CustomerDAO dao=new CustomerDAO();
	   List<CustomerVO> customersList=dao.listCustomers(customerVO);
	%>
	 <table border='1' width='800' align='center'>
	   <tr align='center' bgcolor='#FFFF66'> 
	     <td>์•„์ด๋””</td>
	     <td>๋น„๋ฐ€๋ฒˆํ˜ธ</td>
	     <td>์ด๋ฆ„</td>
	     <td >์ด๋ฉ”์ผ</td>
	     <td>๊ฐ€์ž…์ผ์ž</td>
	</tr>
	<%	
	   for (int i=0; i < customersList.size(); i++){
			CustomerVO vo=(CustomerVO) customersList.get(i);
			String id=vo.getCustId();
	      	String pwd=vo.getPhone();
	      	String name=vo.getCustName();
	      	String addr=vo.getAddr();
	      	String regDate=vo.getRegDate();
	%>
	     <tr align=center>
	       <td><%= id %></td>
	       <td><%= pwd %></td>
	       <td><%= name %></td>
	       <td><%= addr %></td>
	       <td><%=regDate  %></td>
	     </tr>
	   
	<%		
	   }
	%>	
	</table>
	</body>
</html>

el-ignored 

 

 

 


customer1.jsp

	<%@ page language="java" contentType="text/html; charset=UTF-8"
	    pageEncoding="UTF-8" isELIgnored="false"  %>
	<%
	   request.setCharacterEncoding("UTF-8");
	   String custId=request.getParameter("custId");
	   String pwd=request.getParameter("phone");
	   String custName= request.getParameter("custName");
	   String addr= request.getParameter("addr");
	   String birthDate= request.getParameter("birthDate");
	  
	%>   
	<html>
	<head>
	<meta charset="UTF-8">
	<title>ํšŒ์› ์ •๋ณด ์ถœ๋ ฅ์ฐฝ</title>
	</head>
	<body>
	<table border="1"  align="center" >
	    <tr align="center" bgcolor="#99ccff">
	      <td width="20%"><b>์•„์ด๋””</b></td>
	      <td width="20%"><b>๋น„๋ฐ€๋ฒˆํ˜ธ</b></td>
	      <td width="20%" ><b>์ด๋ฆ„</b></td>
	      <td width="20%"><b>์ฃผ์†Œ</b></td>
	      <td width="20%"><b>์ƒ์ผ</b></td>
	   </tr>
	   <tr align=center>
	      <td><%=custId %> </td>
	      <td><%=pwd%> </td>
	      <td><%=custName %> </td>
	      <td><%=addr %> </td>
	      <td><%=birthDate %> </td>
	   </tr>   
	   <tr align=center>
	      <td>${param.custId } </td>
	      <td>${param.phone } </td>
	      <td>${param.custName } </td>
	      <td>${param.addr }</td>
	      <td>${param.birthDate }</td>
	   </tr>
	</table>
	</body>
</html>

 

customerForm.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>ํšŒ์› ๊ฐ€์ž…์ฐฝ</title>
<body>
	<form method="post" action="customer1.jsp">
		<h1 style="text-align: center">ํšŒ์› ๊ฐ€์ž…์ฐฝ</h1>
		<table align="center">
			<tr>
				<td width="200"><p align="right">์•„์ด๋””</td>
				<td width="400"><input type="text" name="custId"></td>
			</tr>
			<tr>
				<td width="200"><p align="right">๋น„๋ฐ€๋ฒˆํ˜ธ</td>
				<td width="400"><input type="password" name="phone"></td>
			</tr>
			<tr>
				<td width="200"><p align="right">์ด๋ฆ„</td>
				<td width="400"><p>
						<input type="text" name="custName"></td>
			</tr>
			<tr>
				<td width="200"><p align="right">์ฃผ์†Œ</td>
				<td width="400"><p>
						<input type="text" name="addr"></td>
			</tr>
			<tr>
				<td width="200"><p align="right">์ƒ์ผ</td>
				<td width="400"><p>
						<input type="text" name="birthDate"></td>
			</tr>
			<tr>
				<td width="200"><p>&nbsp;</p></td>
				<td width="400"><input type="submit" value="๊ฐ€์ž…ํ•˜๊ธฐ"> <input
					type="reset" value="๋‹ค์‹œ์ž…๋ ฅ"></td>
			</tr>
		</table>
	</form>
</body>
</html>

 

 

<%
   request.setCharacterEncoding("UTF-8");
   String custId=request.getParameter("custId");
   String pwd=request.getParameter("phone");
   String custName= request.getParameter("custName");
   String addr= request.getParameter("addr");
   String birthDate= request.getParameter("birthDate");
  
%>   

 

์œ„์—๋Š” ์ด๊ฑฐ๋ฅผ ๊ฐ€์ ธ์™€์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— param์„ ๋งŽ์ด ์“ฐ๊ฒŒ ๋œ๋‹ค.

 


JSTL

JSTL์˜ ์ •์‹ ๋ช…์นญ์€ ์ž๋ฐ”์„œ๋ฒ„ ํŽ˜์ด์ง€ ํ‘œ์ค€ ํƒœ๊ทธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ(JavaServer Pages Standard Tag Library)์ด๊ณ  ์ค„์—ฌ์„œ JSTL์ด๋ผ ๋ถ€๋ฅธ๋‹ค.

 

https://tomcat.apache.org/download-taglibs.cgi

 

Apache Tomcat® - Apache Taglibs Downloads

Welcome to the Apache Taglibs download page. This page provides download links for obtaining the latest version of the Apache Standard Taglib, as well as links to the archives of older releases. You must verify the integrity of the downloaded files. We pro

tomcat.apache.org

 

๋‹ค์šด๋กœ๋“œ ๋ฐ›๊ธฐ 4๊ฐœ๋‹ค webapp์— ๋ถ™์—ฌ๋„ฃ๊ธฐ

 

 

๊ตญ์ œํ™” - ๋‹ค๊ตญ์–ด

 

 


customer2.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
  request.setCharacterEncoding("UTF-8");
%>  

<c:set  var="id"  value="hong"  scope="page" />
<c:set  var="pwd"  value="1234"  scope="page" />
<c:set  var="name"  value="${'ํ™๊ธธ๋™'}"  scope="page" />
<c:set  var="age"  value="${22}"  scope="page" />
<c:set  var="height"  value="${177}"  scope="page" />

<html>
<head>
  <meta charset=”UTF-8">
  <title>ํšŒ์› ์ •๋ณด ์ถœ๋ ฅ์ฐฝ</title>
</head>
<body>
<table  align="center"  border=1  >
   <tr align="center"  bgcolor="lightgreen" >
      <td width="7%" ><b>์•„์ด๋””</b></td>
      <td width="7%" ><b>๋น„๋ฐ€๋ฒˆํ˜ธ</b></td>
      <td width="7%" ><b>์ด๋ฆ„</b></td>
      <td width="7%"><b>๋‚˜์ด</b></td>
      <td width="7%" ><b>ํ‚ค</b></td>
   </tr>
   <tr align="center">
      <td>${id}</td>
      <td>${pwd}</td>
      <td>${name}</td>
      <td>${age}</td>
      <td>${height}</td>
	   </tr>
</table>
</body>
</html>

 


customer3.jsp

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
     import="java.util.*" pageEncoding="UTF-8"  isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
  List<String> dataList=new ArrayList<String>();
  dataList.add("hello");
  dataList.add("world");
  dataList.add("์•ˆ๋…•ํ•˜์„ธ์š”!!");
%>
<c:set  var="list"  value="<%=dataList  %>" />
<html>
<head>
<meta charset=”UTF-8">
<title>๋ฐ˜๋ณต๋ฌธ ์‹ค์Šต</title>
</head>
<body>
   <c:forEach  var="i" begin="1" end="10" step="1"  varStatus="loop">
    i=  ${i}   &nbsp;&nbsp;&nbsp;  ๋ฐ˜๋ณตํšŸ์ˆ˜: ${loop.count} <br>
   </c:forEach>
 <br>
   <c:forEach  var="i" begin="1" end="10" step="2" >
        5 * ${i} = ${5*i}<br>
   </c:forEach>
<br>
   <c:forEach  var="data" items="${list}" >
       ${data } <br> 
   </c:forEach>
<br>
<c:set var="fruits" value="์‚ฌ๊ณผ, ํŒŒ์ธ์• ํ”Œ, ๋ฐ”๋‚˜๋‚˜, ๋ง๊ณ , ๊ทค"  />
<c:forTokens  var="token" items="${fruits}" delims="," >
    ${token} <br> 
</c:forTokens>
	</body>
</html>

 

 

 


MVC Model View Controller 

 

Django์—์„œ๋Š” MVT ํŒจํ„ด..


 

'IT > WEB' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[38์ผ์ฐจ] AOP/ aop concert project  (0) 2022.08.12
[37์ผ์ฐจ] AutoDi  (0) 2022.08.11
[37์ผ์ฐจ] dISample project  (0) 2022.08.11
[37์ผ์ฐจ] Spring Bean  (0) 2022.08.11
[36์ผ์ฐจ] ์Šคํ”„๋ง  (0) 2022.08.10

๋Œ“๊ธ€