예제
src/webapp/login.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>로그인</h1>
<form action="login" method="get">
아이디 : <input type="text" name="id" /><br>
비밀번호 : <input type="text" name="password" /><br>
<input type="submit" value="로그인" />
</form>
</body>
</html>
src/main/java
LoginServlet.jap
import java.io.IOException;
import java.io.PrintWriter;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
/**
* Servlet implementation class LogintServlet
*/
@WebServlet("/login") //servlet class 파일과 url("login") 을 연결 시켜주는 역할
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public LoginServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// request.setCharacterEncoding(”euc-kr”);
String id = request.getParameter("id"); //form 에서 보내는 get 값을 받아옴 id=?, passwd=?
String passwd = request.getParameter("passwd");
response.setContentType("text/html;charset=utf-8"); //응답할 문서 형식, 엔코딩 형식 지정
PrintWriter out = response.getWriter();
out.print("아이디 = "+id+"<br>"); //서버에서 클라이언트로 전달
out.print("비밀번호="+passwd+"<br>");
}
}
request : 요청과 관련된 정보(요청 객체)
response : 응답과 관련된 정보(응답 객체)
Jsp 에서 한글 처리 방식
한글 깨짐 시
server.xml > 63번 라인에 URIEncoding=”utf-8”
추가
<Connector connectionTimeout=*"20000"* port=*"8880"* protocol=*"HTTP/1.1"* redirectPort=*"8443"* URIEncoding=*"utf-8"*/>
request.setCharacterEncoding(”utf-8”);
response.setContentType("text/html;charset=utf-8");