JSP

211020_session

요옫 2021. 10. 20. 16:08

ex1_sessiontest.jsp

<%@page import="java.util.Date"%>

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<link href="https://fonts.googleapis.com/css2?family=Do+Hyeon&display=swap" rel="stylesheet">

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">

<script src="https://code.jquery.com/jquery-3.5.0.js"></script>

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>

<title>Insert title here</title>

</head>

<body>

<%

//세션을 저장하는 방법

session.setAttribute("msg", "Happy Day!");

session.setMaxInactiveInterval(60);  //60초의 유지시간

%>

 

<h3>세션값 얻기</h3>

세션값: <%=session.getAttribute("msg") %> <br>

세션유지시간: <%=session.getMaxInactiveInterval() %><br>

세션생성시간: <%=session.getCreationTime() %> <br>

세션생성시간: <%=new Date(session.getCreationTime()) %>

</body>

</html>

 

 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 

 

ex2_sessionmain

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<link href="https://fonts.googleapis.com/css2?family=Do+Hyeon&display=swap" rel="stylesheet">

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">

<script src="https://code.jquery.com/jquery-3.5.0.js"></script>

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>

<title>Session Main</title>

</head>

<body>

<%

//세션저장

session.setAttribute("msg", "Happy");

session.setMaxInactiveInterval(10);  //10초 유지시간

%>

 

<h2>10초 안에 눌러주세요!!</h2>

  <form action="ex3_sessionaction.jsp" method="post">

    <h2>받고싶은 상품은?</h2>

    <input type="radio" value="아이폰" name="gift" checked="checked">아이폰&nbsp;

    <input type="radio" value="갤럭시폴드" name="gift">갤럭시폴드&nbsp;

    <input type="radio" value="맥북" name="gift">맥북&nbsp;

    <input type="radio" value="아이패드" name="gift">아이패드&nbsp;

    

    <input type="submit" value="상품선택">

  </form>

</body>

</html>

 

 

ex3_sessionaction

package idx.model;

 

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

 

import mysql.db.DbConnect;

 

public class idxDao {

 

DbConnect db=new DbConnect();

 

//아이디를 통해서 name값을 얻는다..파라메타값을 아이디로

public String getName(String id) {

Connection conn=db.getConnection();

PreparedStatement pstmt=null;

ResultSet rs=null;

 

String sql="select * from idx where id=?";

String name="";

 

try {

pstmt=conn.prepareStatement(sql);

 

//?바인딩

pstmt.setString(1, id);

 

rs=pstmt.executeQuery();

 

//하나만 구할거니까 if

if(rs.next())

{

name=rs.getString("name");

}

 

} catch (SQLException e) {

} finally {

db.dbClose(rs, pstmt, conn);

}

return name;

}

}

 

 

'JSP' 카테고리의 다른 글

211021_파일입출력(여러개)  (0) 2021.10.21
211021_파일입출력  (0) 2021.10.21
211020_session으로 로그인/로그아웃  (0) 2021.10.20
211020_jsp로 게시판 만들기  (0) 2021.10.20
211019_복습(jsp)  (0) 2021.10.19