BASHA TECH
ch05-06 본문
728x90
계산기 프로그램
src/main/java > ch05 > CalcServlet.java
src > main > java > ch05 > CalcServlet.java
package ch05;
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;
@WebServlet("/ch05/calc")
public class CalcServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public CalcServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int n1 = Integer.parseInt(request.getParameter("n1"));
int n2 = Integer.parseInt(request.getParameter("n2"));
String op = request.getParameter("op");
long result = 0;
switch(op) {
case "add":
result = n1 + n2;
break;
case "sub":
result = n1 - n2;
break;
case "mul":
result = n1 * n2;
break;
case "div":
result = n1 / n2;
break;
}
// client 결과 보낸다. => response 사용
response.setContentType("text/html; charset=utf-8"); // 이 위치에 정확히 있어야함
// utf-8로 인코딩하라고 먼저 선언.
PrintWriter out = response.getWriter();
out.println("<h2>계산기</h2><hr>");
out.println("계산 결과: " + result);
}
}
src > ch05 >calForm.jsp
<%@ 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>
<h2>계산기~</h2>
<hr>
<form action="calc" method="post">
<input type="text" name="n1" size="10">
<select name="op">
<option value="add">+</option>
<option value="sub">-</option>
<option value="mul">x</option>
<option value="div">/</option>
</select>
<input type="text" name="n2" size="10">
<input type="submit" value="계산">
</form>
</body>
</html>
Hello World
src/main/java > ch05 > HelloWorld.java
src > main > java > ch05 > HelloWorld.java
package ch05;
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;
@WebServlet("/hello01")
public class HelloWorld extends HttpServlet {
private static final long serialVersionUID = 1L;
public HelloWorld() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 한글 처리 => 첫 줄에 존재
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println("<!doctype html>");
out.println("<html>");
out.println("<head>");
out.println("</head>");
out.println("<body>");
out.println("<h2>Hell World~! <h2>"); // 화면 담당은 jsp 처리담당은 servlet이~
out.println("</body>");
out.println("</html>");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
}
회원가입 프로그램
src/main/java > kr.co.big15 > LoginServlet.java
src > main > java > ch05 > kr > co > big15 > LoginServlet.java
package kr.co.big15;
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;
import javax.servlet.http.HttpSession;
/**
* Servlet implementation class LoginServlet
*/
@WebServlet("/login/LoginServlet")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public LoginServlet() {
super();
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
// 로그인 여부 확인 =>
HttpSession session = request.getSession();
// if (session.getAttribute("userid") == null) {
// response.sendRedirect("loginForm.jsp");
// return;
// }
String userid = request.getParameter("userid"); //request 들어있는 id 값을 추출
String userpw = request.getParameter("userpw"); //request 들어있는 pw 값을 추출
// id/pw 데이터 검증 (validation)
if (userid.isEmpty() || userpw.isEmpty()) {
response.sendRedirect("loginForm.jsp");
return;
}
// getSession(true) : client가 가지고 있는 세션ID와 동일한 세션 object가 있으면 찾아서 주소값을 반환한다.
// 없으면 생성한다.
// getSession(false) : 새로 생성을 하지 않습니다.
PrintWriter out = response.getWriter();
// userid, userpw => DataBase select 처리
if (userid.equals("hong")) {
if (userpw.equals("1234")) {
// userid, userpw 정상 입력 => 메인페이지 이동
// session을 생성하고, session userid를 저장 => 메인페이지에서 userid 출력
session.setAttribute("userid", userid);
session.setAttribute("usernm", "홍길동");
response.sendRedirect("../main.jsp");
}else {
// userpw 값이 잘못된 경우 처리할 코드
out.println("<script>alert('Password Error!'); history.back();</script>");
}
}else {
// userid 값이 잘못된 경우 처리할 코드
out.println("<script>alert('ID Error!'); history.back();</script>");
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
}
src/main/java > kr.co.big15 > LogoutServlet.java
src > main > java > ch05 > kr > co > big15 > LogoutServlet.java
package kr.co.big15;
import java.io.IOException;
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("/LogoutServlet")
public class LogoutServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public LogoutServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 로그아웃 => Session 저장된 데이터 삭제
HttpSession session = request.getSession(false);
// session.removeAttribute("userid");
// session.removeAttribute("usernm");
if (session != null && session.getAttribute("userid") != null) {
session.invalidate(); // 세션에 저장된 전체 데이터를 한번에 삭제
}
response.sendRedirect("./login/loginForm.jsp");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
}
src > main > webapp > login > loginForm.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>로그인</title>
</head>
<body>
<div>
<h2>로그인</h2>
<form action="LoginServlet" method="get" name="form1">
ID <input type="text" name="userid"><br>
PW <input type="text" name="userpw">
<input type="submit" value="login">
</form>
</div>
</body>
</html>
src > main > webapp > main.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>BIG15 메인</title>
</head>
<body>
<h2>메인 페이지</h2>
${usernm} 님 환영합니다! <a href="#">로그아웃</a>
</body>
</html>
728x90
반응형
Comments