Dynamic Web Project-FirstSevlet
Next> 누르다가 체크
기존 프로젝트의 META-INF에 있는 context.xml 파일을 복사해서 붙여넣기
-
exam이라는 폴더를 만들고 adder라고 하는 jsp만들기. addResult.jsp를 하나더 만들어서 입력한 num1과 num2의 덧셈결과 뽑기.
-
adder.jsp: 숫자 덧셈 폼
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="addResult.jsp">
첫번째 수 : <input type="text" name="num1"><br>
두번째 수 : <input type="text" name="num2"><br>
<input type="submit" value="더하기">
</form>
</body>
</html>
-
addResult.jsp: 덧셈결과 로직
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<%
request.setCharacterEncoding("utf-8");
int num1=Integer.parseInt(request.getParameter("num1"));
int num2=Integer.parseInt(request.getParameter("num2"));
int result=num1+num2;
%>
</head>
<body>
덧셈결과: <%=result%><br>
뺄셈결과: <%=num1-num2 %><br>
</body>
</html>
-
adder2.jsp 만들고 form action="adder" 라고 바꾸기. adder를 어떻게 찾을까?
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="adder">
첫번째 수 : <input type="text" name="num1"><br>
두번째 수 : <input type="text" name="num2"><br>
<input type="submit" value="더하기">
</form>
</body>
</html>
-
FirstServlet의 WEB-INF 폴더에 있는 web.xml 파일을 다음과 같이 수정하기
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
<display-name>FirstServlet</display-name>
<!-- adder -->
<servlet>
<servlet-name>adder-servlet</servlet-name>
<servlet-class>com.exam.action.AdderServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>adder-servlet</servlet-name>
<url-pattern>/exam/adder</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
-
java 패키지 com.exam.action 생성 후 AdderServlet.java 만들기
package com.exam.action;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class AdderServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
int num1=Integer.parseInt(req.getParameter("num1"));
int num2=Integer.parseInt(req.getParameter("num2"));
int sum=num1+num2;
resp.setContentType("text/html;charset=utf-8");
PrintWriter out=resp.getWriter(); //출력객체
out.println("<html>");
out.println("<head><title>결과</title></head>");
out.println("<body>");
out.println("덧셈결과:"+sum);
out.println("</body>");
out.println("</html>");
}
}
-
adder2.jsp에서 jsp는 더이상 필요없고 자바 파일인 AdderServlet이 덧셈을 수행한다.
-
bbs.jsp: 메모 작성 폼 (작성자, 제목, 내용)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="bbs.do" method="post">
이름: <input type="text" name="name"><br>
제목: <input type="text" name="title"><br>
내용: <br>
<textarea rows="5" cols="20" name="content"></textarea>
<input type="submit" value="전송">
</form>
</body>
</html>
-
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
<display-name>FirstServlet</display-name>
<!-- bbs -->
<servlet>
<servlet-name>bbs-servlet</servlet-name>
<servlet-class>com.exam.action.BbsServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>bbs-servlet</servlet-name>
<url-pattern>/exam/bbs.do</url-pattern>
</servlet-mapping>
<!-- adder -->
<servlet>
<servlet-name>adder-servlet</servlet-name>
<servlet-class>com.exam.action.AdderServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>adder-servlet</servlet-name>
<url-pattern>/exam/adder</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
-
BbsServlet.java: 결과값 출력
package com.exam.action;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class BbsServlet extends HttpServlet{
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
String name=req.getParameter("name");
String title=req.getParameter("title");
String content=req.getParameter("content");
resp.setContentType("text/html;charset=utf-8");
PrintWriter out=resp.getWriter();
out.println("<html>");
out.println("<head><title></title></head>");
out.println("<body>");
out.println("작성자: "+name+"<br>");
out.println("제목: "+title+"<br>");
out.println("내용: "+content+"<br>");
out.println("</body>");
out.println("</html>");
}
}