gwooden_코린이

이클립스 서블릿 쿼리스트링 본문

JSP&Servlet

이클립스 서블릿 쿼리스트링

gwooden22 2023. 1. 17. 18:31
728x90

1. 쿼리스트링

클라이언트(사용자) -> 값을 전달 (서버)

https://search.naver.com/search.naver?where=nexearch&sm=top_hty&fbm=1&ie=utf8&query=%EC%9E%90%EB%B0%94


<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
	<form method="get" action="qs">
		횟수 입력 : <input type="text" name="cnt">
		<input type="submit" value="전송">
	</form>
</body>
</html>
package unit01;

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 QueryString
 */
@WebServlet("/qs")
public class QueryString extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public QueryString() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=utf-8");
		
		//request 문자로 전달되므로 형변환 시켜야함
		int c = Integer.parseInt(request.getParameter("cnt"));
		
		PrintWriter out = response.getWriter();
		
		for(int i=0; i<c; i++) {
			out.print("안녕<br>");
		}
	}

	/**
	 * @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);
	}

}

 

서버 실행 후 입력창에 원하는 숫자를 입력 후 전송 버튼을 누르

 

주소창에 클라이언트가 전달 한 값을 서버가 처리하면서 주소값도 알맞게 바뀌고

값도 정상적으로 출력

 

  • get : 길이제한 255 (보안x)
  • post 길이제한 없음 (보안o)

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
	<form method="post" action="qs">
		이름 입력 : <input type="text" name="name">
		나이 입력 : <input type="text" name="age">
		<input type="submit" value="전송">
		
	</form>
</body>
</html>
package unit01;

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 QueryString
 */
@WebServlet("/qs")
public class QueryString extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public QueryString() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("utf-8");
	    response.setContentType("text/html; charset=utf-8");
		
		//리스폰 요청받은거 반환
		PrintWriter out = response.getWriter();
		
		
		//request 문자로 전달되므로 형변환 시켜야함
		String name = request.getParameter("name");

		
		
		int c = Integer.parseInt(request.getParameter("age"));
//		out.printf("이름 : %s\n <br> 나이  : %d\n <br>", name, c);
		
		out.print("이름 : " + name + "<br>");
	    out.print("나이 : " + c + "<br>");

		
		if(c >= 20) {
			out.print("비고 : 성인");
		} else {
			out.print("비고 : 청소년");
		}

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		doGet(request, response);
	}

}

 

728x90
Comments