반응형
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="jspservlet.vo.*" %>
<%
UserVO loginUser = null;
if(session.getAttribute("loginUser") != null){
loginUser = (UserVO)session.getAttribute("loginUser");
}
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>index</title>
</head>
<body>
<h2>서블릿 연습 프로젝트!</h2><hr>
<h3>가상경로 연습하기</h3>
<a href="<%= request.getContextPath() %>/seconde.do">seconde.do로 이동하기</a>
<!-- 화면에 seconde.do servlet page!! 출력 -->
|
<a href="<%= request.getContextPath() %>/board/list.do">/board/list.do로 이동하기</a>
<!-- 화면에 board/list.do servlet page!! 출력 -->
<br>
<%
if(loginUser != null){
%>
[<strong><%= loginUser.getUid() %></strong>]님 환영합니다.
<a href="<%= request.getContextPath() %>/logout.do">로그아웃</a>
<a href="<%= request.getContextPath() %>/mypage.do">내 정보 확인</a>
<%
}else{
%>
<a href="<%= request.getContextPath() %>/login.do">로그인</a>
<%
}
%>
</body>
</html>
mypage.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="jspservlet.vo.*" %>
<%
//request의 attribute 값은 object타입이므로 강제 형변환 필요
UserVO loginUser = (UserVO)request.getAttribute("loginUser");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>mypage</title>
</head>
<body>
<h2>마이페이지</h2>
회원번호 : <span><%= loginUser.getUno() %> </span><br>
회원아이디 : <span> <%= loginUser.getUid() %></span><br>
회원비밀번호 : <span> <%= loginUser.getUpw() %></span><br>
회원이름 : <span><%= loginUser.getUname() %> </span><br>
회원연락처 : <span><%= loginUser.getUphone() %> </span><br>
회원이메일 : <span><%= loginUser.getUemail() %> </span><br>
회원가입일 : <span><%= loginUser.getRdate() %> </span><br>
회원상태 : <span><%= loginUser.getUstate() %> </span><br>
회원권한 : <span><%= loginUser.getUauthor() %> </span><br>
</body>
</html>
MypageController.java
package jspservlet.controller;
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;
import jspservlet.util.DBConn;
import jspservlet.vo.UserVO;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
/**
* Servlet implementation class MypageController
*/
@WebServlet("/mypage.do")
public class MypageController extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public MypageController() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
UserVO loginUser = (UserVO)session.getAttribute("loginUser");
String uno = loginUser.getUno();
Connection conn = null; //DB 연결
PreparedStatement psmt = null; //SQL 등록 및 실행. 보안이 더 좋음!
ResultSet rs = null; //조회 결과를 담음
//try 영역
try{
conn = DBConn.conn();
String sql = "select * from user where uno=?";
psmt = conn.prepareStatement(sql);
psmt.setString(1, uno);
rs = psmt.executeQuery();
if(rs.next()){
loginUser.setUpw(rs.getString("upw"));
loginUser.setUphone(rs.getString("uphone"));
loginUser.setUemail(rs.getString("uemail"));
loginUser.setRdate(rs.getString("rdate"));
loginUser.setUstate(rs.getString("ustate"));
request.setAttribute("loginUser", loginUser);
request.getRequestDispatcher("/user/mypage.jsp").forward(request, response);
}else {
//회원조회 실패할 경우
response.sendRedirect(request.getContextPath()+"/login.do");
}
}catch(Exception e){
e.printStackTrace();
}finally{
try {
DBConn.close(rs, psmt, conn);
}catch(Exception e) {
e.printStackTrace();
}
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
반응형
'Java' 카테고리의 다른 글
[Servlet] 공지게시글 상세페이지 조회 (0) | 2024.10.12 |
---|---|
[Servlet] 공지 게시글 목록 조회하기 (0) | 2024.10.11 |
[Servlet] 로그인 처리 (0) | 2024.10.09 |
[JSP] 회원정보 조회하고 수정하기 (0) | 2024.10.08 |
[Servlet] 모델2 MVC (4) | 2024.10.07 |