스크립트릿
- 로그인
- sLogin.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
request.setCharacterEncoding("utf-8");
String id = request.getParameter("user_id");
String pw = request.getParameter("user_pw");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
if (id==null || id.length()==0){
%>
<p>아이디를 입력해주세요</p>
<a href="login.html">로그인 하기</a>
<%
} else if (pw==null || pw.length()==0){
%>
<p>비밀번호를 입력해주세요</p>
<a href="login.html">로그인 하기</a>
<%
} else {
%>
<h2>환영합니다 <%=id %>님!</h2>
<%} %>
</body>
</html>
//아이디 미입력
아이디를 입력해주세요
로그인 하기
//아이디 입력
환영합니다 aa님!
- 로그인
아이디 or 비번 미입력 -> 아이디와 비밀번호는 필수입니다 & 로그인하기
로그인 -> 환영합니다 id 님
아이디 admin -> 관리자로 로그인했습니다 & 회원정보 수정하기, 회원정보 삭제하기
- aLogin.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
request.setCharacterEncoding("utf-8");
String id = request.getParameter("user_id");
String pw = request.getParameter("user_pw");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>스크팁트릿 연습</title>
</head>
<body>
<%
if (id==null || id.length()==0 || pw==null || pw.length()==0){
%>
<p>아이디와 비밀번호는 필수입니다</p>
<a href="login.html">로그인하기</a>
<%
} else if (id.equals("admin")){
%>
<p>관리자로 로그인했습니다</p>
<input type="button" value="회원정보 수정">
<input type="button" value="회원정보 삭제">
<%} else { %>
<p>환영합니다 <%=id %>님!</p>
<%} %>
</body>
</html>
문자열 비교는 equals() 메소드 사용하기
- 등급 변환
90~100 A
80~89 B
70~79 C
60~69 D
F
- score.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>로그인 창</title>
</head>
<body>
<form action="score.jsp" method="post" name="formScore">
시험점수 <input type="text" name="score">
<input type="submit" value="변환">
</form>
</body>
</html>
- score.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%! String level=""; %>
<%
int score = Integer.parseInt(request.getParameter("score"));
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>점수</title>
</head>
<body>
<%
switch (score/10) {
case 9: case 10 :
level="A";
break;
case 8:
level="B";
break;
case 7:
level="C";
break;
case 6:
level="D";
break;
default:
level="F";
break;
}
%>
<p>결과 : <%=level %> 등급</p>
</body>
</html>
- 구구단
- gugu.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>구구단 입력창</title>
</head>
<body>
<form action="gugu.jsp" method="post" name="formScore">
<h2>원하는 단을 입력해주세요</h2>
단 : <input type="text" name="dan">
<input type="submit" value="입력">
</form>
</body>
</html>
- gugu.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
int dan = Integer.parseInt(request.getParameter("dan"));
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>구구단</title>
</head>
<body>
<table border="1" width="400">
<tr align="center" bgcolor="azure">
<th colspan="2">** <%=dan %>단 **</th>
</tr>
<%
for (int i=1;i<=9;i++){
if (i%2==0){
%>
<tr align="center" bgcolor="beige">
<% } else { %>
<tr align="center" bgcolor="whitesmoke">
<% } %>
<td width="200">
<%=dan %> X <%=i %>
</td>
<td width="200">
<%=dan*i %>
</td>
</tr>
<%} %>
</table>
</body>
</html>
- 이미지 리스트
- image_list.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String[] name={"냉장고","컴퓨터","청소기","세탁기","에어컨"};
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>상품 정보 리스트</title>
<style>
h2 {
text-align:center;
}
table{
width: 600px;
margin : 0 auto;
}
table .head{
background-color : lime;
}
tr {
text-align : center;
}
td {
width:200px;
}
</style>
</head>
<body>
<h2>상품 정보 리스트</h2>
<table border="1">
<tr class="head">
<th>상품이미지</th><th>상품이름</th><th>선택하기</th>
</tr>
<%
for (int i=0;i<5;i++){
%>
<tr>
<td><img src="images/<%=i%>.jpg" alt="" width="100" height="100"></td>
<td>이미지 이름 : <%=name[i] %></td>
<td><input type="checkbox" name="check"></td>
</tr>
<%} %>
</table>
</body>
</html>
- BMI
이름 미입력시 => 이름을 입력해주세요 & bmi.jsp로 돌아가기
표준몸무게 -5 ~+5 => 철수님은 00kg로 정상 몸무게입니다 / 비만입니다 / 저체중입니다
표준몸무게 =( 키 - 100 ) * 0.9
- login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>BMI</title>
</head>
<body>
<form action="bmiResult.jsp" method="post">
이름 : <input type="text" name="name"><br>
키 : <input type="text" name="height"><br>
몸무게 : <input type="text" name="weight"><br>
<input type="submit" value="계산하기">
</form>
</body>
</html>
- bmiResult.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
request.setCharacterEncoding("utf-8");
String name = request.getParameter("name");
int height = Integer.parseInt(request.getParameter("height"));
int weight = Integer.parseInt(request.getParameter("weight"));
int standard = (int)((height-100) * 0.9);
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>BMI</title>
</head>
<body>
<%
if (name==null || name.length()==0){
%>
<h2>이름을 입력해주세요</h2>
<a href='bmi.jsp'>비만도 체크</a>
<%
} else if (standard-5<=weight&&weight<=standard+5) {
%>
<h2><%=name %>님은 <%=weight %>kg으로, 정상몸무게입니다</h2>
<%
} else if (weight<standard-5){
%>
<h2><%=name %>님은 <%=weight %>kg으로, 저체중입니다</h2>
<%
} else {
%>
<h2><%=name %>님은 <%=weight %>kg으로, 비만입니다</h2>
<%
}
%>
</body>
</html>
- 세션 바인딩
- SessionTest.java
@WebServlet("/ses1")
public class SessionTest extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
HttpSession session = request.getSession();
session.setAttribute("name","홍길동");
out.print("<html><body>");
out.print("<h2>세션에 이름 바인딩</h2>");
out.print("<a href='/jsp01/bindTest/session1.jsp'>첫번째 페이지로 이동</a>");
out.print("</body></html>");
}
}
- session1.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String name = (String)session.getAttribute("name");
session.setAttribute("address", "서울시 종로구");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>session 바이딩 테스트</title>
</head>
<body>
이름은 <%=name %>입니다
<a href="session2.jsp">두번째 페이지 이동</a>
</body>
</html>
- session2.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String name = (String) session.getAttribute("name");
String address = (String) session.getAttribute("address");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>세션 바인딩 실습</title>
</head>
<body>
<h2>이름은 <%=name %>입니다</h2>
<h2>주소는 <%=address %>입니다</h2>
</body>
</html>
//
http://localhost:8080/jsp01/ses1
세션에 이름 바인딩
첫번째 페이지로 이동
이름은 홍길동입니다 두번째 페이지 이동
이름은 홍길동입니다
주소는 서울시 종로구입니다
https://victorydntmd.tistory.com/155
https://jerryjerryjerry.tistory.com/32
Scope (영역)
jsp 페이지 내 객체들은 정해진 영역 안에서만 사용 가능
1. page 영역
하나의 jsp 페이지를 범위로 가짐
<% %> 안에 변수를 사용하면 이 변수는 해당 JSP 파일 내에서만 유효
2. request 영역
요청을 받아서 응답하기까지 객체가 유효한 영역
forward / include => request 요청 객체가 공유되어 request 영역이 됨
3. session 영역
하나의 웹 브라우저와 관련된 영역
브라우저당 1개의 session 객체 => 같은 브라우저 내에서 요청되는 페이지들은 같은 객체 공유
4. application 영역
하나의 웹 어플리케이션과 관련된 전체 영역을 포함
같은 애플리케이션 내에서 요청되는 페이지들은 같은 객체 공유
※ JSP 내장 객체 : session, application, request,..
=> 새로 객체 생성하지 않아도 사용할 수 있음
- session, application
- appTest1.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
session.setAttribute("name", "박지성"); <!--같은 브라우저 내 공유-->
application.setAttribute("address", "서울시 중구"); <!--웹앱 종료까지 공유(같은 서버에서 공유)-->
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>스코프 테스트</title>
</head>
<body>
<h2>이름, 주소 저장</h2>
<a href="appTest2.jsp">두번째 페이지 이동</a>
</body>
</html>
- appTest2.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String name = (String) session.getAttribute("name");
String address = (String) application.getAttribute("address");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>이름은 <%=name %>입니다</h2>
<h2>주소는 <%=address %>입니다</h2>
</body>
</html>
// 다른 브라우저에서 실행 시
이름은 null입니다
주소는 서울시 중구입니다
- requestDispatcher 사용하여 request 전달
=> 서버상에서 다른 서블릿으로 다이렉트로 요청 전달 (응답 X)
=> 다음 서블릿이 응답하기 전까지 살아 있음
- requestTest1.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
request.setAttribute("name", "이영희");
request.setAttribute("address", "서울시 서초구");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>request 테스트</title>
</head>
<body>
<%
RequestDispatcher d = request.getRequestDispatcher("requestTest2.jsp"); //포워딩
d.forward(request, response);
%>
</body>
</html>
- requestTest2.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String name = (String)request.getAttribute("name");
String address = (String)request.getAttribute("address");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>request 테스트</title>
</head>
<body>
<h2>이름은 <%=name %>입니다</h2>
<h2>주소는 <%=address %>입니다</h2>
</body>
</html>
=>
http://localhost:8080/jsp01/bindTest/requestTest1.jsp
=> 포워딩됨
이름은 이영희입니다
주소는 서울시 서초구입니다
'Programming > 국비학원' 카테고리의 다른 글
221006 - JSP - 액션태그, EL (0) | 2022.10.07 |
---|---|
221005 - JSP - 예외 처리, welcome 페이지, 액션 태그 (0) | 2022.10.06 |
220929 - 세션 바인딩, JSP (0) | 2022.09.30 |
220928 - 서블릿 - 쿠키, 세션 (0) | 2022.09.29 |
220927 - 서블릿 - 서블릿 확장 API (ServletContext 클래스), 쿠키, 세션 (0) | 2022.09.28 |