| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | 7 |
| 8 | 9 | 10 | 11 | 12 | 13 | 14 |
| 15 | 16 | 17 | 18 | 19 | 20 | 21 |
| 22 | 23 | 24 | 25 | 26 | 27 | 28 |
- 웹개발
- Java
- job
- BCIT
- react
- SpringFramework
- MSA
- Doit알고리즘코딩테스트
- vite
- DevOps
- Grafana
- spring boot
- 웹개발자
- coding test
- MVC패턴
- jsp
- html
- 서버 모니터링
- Bambino
- 시큐어코딩
- two pointers
- Spring MVC
- 데이터베이스
- 모니터링
- sql
- CSS
- 웹개발기초
- 웹 보안
- 자바
- servlet
- Today
- Total
초보 개발자의 기록
JSP 내장객체(Built-In Object) 본문

JSP에서만 지원하는 내장 객체(Built-In Object)를 학습해보자
내장 객체는 이미 내부적으로 인스턴스가 지원되면서, 해당 인스턴스의 변수명까지 이미 정해진 상태이다.
1. request : 클라이언트의 요청 정보를 저장하고 있는 객체
2. response : 클라이언트에게 보낼 응답 정보를 가진 객체
3. out : jsp에 출력을 담당하는 객체, 응답 페이지 전송을 위한 출력 stream
4. pageContext : 응답 페이지 실행에 필요한 Context정보를 저장한 객체
5. session : 정보를 세션 범위에서 유지하기 위해 지원되는 객체(로그인 정보 처리 시 사용할 예정)
6. application : 정보를 애플리케이션 범위에서 유지하기 위해 지원되는 객체, 동일한 Application의 context정보를 저장
7. page : jsp를 표현한 객체, 특정 페이지의 서블릿 객체(인스턴스화 된 객체)
8. config : jsp의 설정 정보를 보유한 객체
9. exception : jsp에서 발생한 예외정보를 보유한 객체, 예외처리를 위한 객체
<%@ page contentType="text/html;charset=utf-8"%>
<%@ page import="java.sql.DriverManager"%>
<%@ page import="java.sql.Connection"%>
<%@ page import="java.sql.PreparedStatement"%>
<%@ page import="java.sql.ResultSet"%>
<%
//request와 response 이용한 로그인 처리
//클라이언트가 전송한 id,pass 파라미터를 받아와 처리해보자
//request 객체를 이용하면, 클라이언트의 요청을 처리할 수 있다.
String id = request.getParameter("id"); //클라이언트가 요청시 전송한 파라미터값을 얻는 메서드
// 매개변수로는 파라미터 변수명을 명시,
//만일 html 이용할 경우 컴포넌트에 부여된 name값을 명시
String pass = request.getParameter("pass");
out.print("현재 클라이언트의 요청방식은"+ request.getMethod());
out.print("<br>");
out.print("클라이언트가 전송한 id는 "+id);
out.print("<br>");
out.print("클라이언트가 전송한 pass는 "+pass);
//드라이버 로드
//모든 jar 파일은 JavaEE기반의 스펙을 따라서, 위치시켜야한다
//스펙에 의하면 class파일과 jar는 WEB-INF라는 대문자로 된 보안된 디렉토리에 위치시켜야한다.
//클래스의 위치는 WEB-INF/classes, jar는 WEB-INF/lib에 둬야한다.
Class.forName("oracle.jdbc.driver.OracleDriver");
//접속
String url="jdbc:oracle:thin:@localhost:1521:XE";
String user="user1104";
String password="user1104";
Connection con = DriverManager.getConnection(url, user, password);
PreparedStatement pstmt = null;
ResultSet rs = null;
if(con == null){
out.print("접속 실패");
}else{
out.print("접속 성공");
//쿼리문 수행
String sql ="SELECT * FROM shop_member WHERE mid=? and pass=?";
pstmt = con.prepareStatement(sql); //쿼리준비
pstmt.setString(1, id);
pstmt.setString(2, pass);
rs = pstmt.executeQuery();
if(rs.next()){ //회원이 있다면, 로그인 성공으로 간주
out.print("로그인 성공");
}else{
out.print("로그인 정보가 올바르지 않습니다");
}
}
if(rs!= null){
rs.close();
}
if(pstmt!= null){
pstmt.close();
}
if(con!=null){
con.close();
}
%>
get방식으로는 id, pass를 가지고 조작이 가능함
localhost:9090/member/login.jsp?id=scott&pass=8000

코드만 보고서는 어떤 순서로 진행이 되는지 기억할 수 없기 때문에ㅜㅜ
DB 연동
드라이버 로드 : Class.forName("orcal:jdbc:driver.OracleDriver");
(EditPlus로 개발시)
WEB-INF/lib에 jar 넣어주기 : 넣어주기 전에는 ClassFoundException이 발생함
C:\oraclexe\app\oracle\product\11.2.0\server\jdbc\lib> ojddbc6.jar를
WEB-INF/lib에 넣어주기 후 톰캣 재가동
//접속
String url = "jdbc:oracleString url="jdbc:oracle:thin:@localhost:1521:XE";
String user="";
String password="";
DriverManager.getConnection(url, user, password);
DriverManager cannot be resolved
페이지 지시어가 import담당함
<%@ page import="java.sql.DriverManager"%>
Connection con = DriverManager.getConnection(url, user, password);
<%@ page import="java.sql.Connection"%>
if(con==null){
out.print("접속 실패");
}else{
out.print("접속 성공");
//쿼리문
String sql = "SELECT * FROM shop_member WHERE mid=? and pass=?";
}
jsp에서는 예외처리를 내부적으로 하기때문에 따로 입력하지 않아도 된다
PreParedStatement pstmt = null;
<%@ page import="java.sql.PreParedStatement"%>
//쿼리준비 pstmt = con.prepareStatement(sql);
ResultSet rs = null;
<%@ page import="java.sql.ResultSet "%>
rs = pstmt.executeQuery();
if(rs.next()){
out.print("로그인 성공");
}else{
out.print("로그인 정보가 올바르지 않습니다");
}
if(rs!=null){
rs.close();
}
if(pstmt!=null){
pstmt.close();
}
if(con!=null){
con.close();
}
//쿼리문 수행 안의 영역
pstmt.setString(1, id);
pstmt.setString(2, pass);

jenkwon92/JSP_Workspace
Korea it academy JSP class. Contribute to jenkwon92/JSP_Workspace development by creating an account on GitHub.
github.com
'Backend > JSP' 카테고리의 다른 글
| 게시판만들기(EditPlus) (0) | 2020.12.06 |
|---|---|
| 로그인폼 만들기 (0) | 2020.12.04 |
| JSP 작동 원리 (0) | 2020.12.03 |
| JSP기초 (0) | 2020.12.01 |
| 웹어플리케이션 설명 & Tomcat설치 (0) | 2020.12.01 |