티스토리 뷰
반응형
서블릿 응답
- 서블릿 요청 시 PrintWriter를 이용해 html을 응답할 수 있다.
- 예제 서블릿 login2.java를 작성한다.
* src/servlet/Login2.java
package servlet;
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;
/**
* Servlet implementation class Login2
*/
@WebServlet("/login2")
public class Login2 extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8"); // 응답할 데이터가 HTML임을 설정
PrintWriter out = response.getWriter();
String userId = request.getParameter("user_id");
String userPw = request.getParameter("user_pw");
String data = "<html>";
data += "<body>";
data += "아이디 : " + userId;
data += "<br />";
data += "비밀번호 : " + userPw;
data += "</body>";
data += "</html>";
out.print(data);
}
}
- 테스트할 로그인 페이지를 작성한다.
* WebContent/login.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Login</title>
</head>
<body>
<form name="frmLogin" method="get" action="login2" encType="utf-8">
<label>아이디</label>
<input type="text" name="user_id" />
<br />
<label>비밀번호</label>
<input type="password" name="user_pw" />
<br />
<button type="submit">로그인</button>
</form>
</body>
</html>
- 작성 후 브라우저에서 localhost:[port]/[project_name]/login.html을 입력해 로그인 페이지에 접속하여 아이디와 비밀번호를 입력하고 [로그인] 버튼을 누른다.
- 값을 입력하고 로그인 버튼을 누르면 서블릿이 html 데이터를 응답한다.
반응형
'Servlet' 카테고리의 다른 글
서블릿 - 비즈니스 로직 처리 (0) | 2024.08.11 |
---|---|
GET/POST 전송 방식 (0) | 2024.08.11 |
서블릿 - 클라이언트 요청 얻기 (0) | 2024.08.11 |
서블릿의 기능 (0) | 2024.08.11 |
서블릿 매핑 (0) | 2024.08.11 |
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- html css
- Session
- 서블릿
- 리액트
- Binding
- nodejs
- 네트워크
- 스프링
- 서브넷팅
- Spring Security
- JSP
- react
- el
- 제이쿼리
- httpServletRequest
- Redux
- 인가
- 미들웨어
- 내장객체
- 스프링 시큐리티
- script element
- CSS
- Network
- CSS 속성
- Java Server Page
- Spring
- HTML
- 세션
- Servlet
- javaserverpage
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 29 | 30 |
글 보관함