[JSP/Servlet] 텍스트 박스의 값 얻어오기 예제

param.jsp

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

    pageEncoding="UTF-8"%>


<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>서블릿</title>

<script type="text/javascript" src="param.js"></script>

</head>

<body>

<form action="ParamServlet" method="get" name="frm">

아이디 : <input type="text" name="id"><br>

나&nbsp;이 : <input type="text" name="age"><br>

<input type="submit" value="전송" onclick="return check()">

</form>

</body>

</html>


ParamServlet.java

package unit03;


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;


@WebServlet("/ParamServlet")

public class ParamServlet extends HttpServlet {

       

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

response.setContentType("text/html;charset=UTF-8");

String id = request.getParameter("id");

int age = Integer.parseInt(request.getParameter("age"));

PrintWriter out = response.getWriter();

out.print("<html><body>");

out.println("당신이 입력한 정보입니다.<br>");

out.println("아이디 : ");

out.println(id);

out.println("<br> 나이 : ");

out.println(age);

//자바스크립트로 이전 페이지로 이동하는 링크 만들기

out.println("<br><a href='javascript:history.go(-1)'>다시</a>");

out.print("</body></html>");

out.close();

}

}


param.js

function check(){

if(document.frm.id.value==""){

alert("아이디를 입력해 주세요");

document.frm.id.focus();

return false;

}else if(document.frm.age.value==""){

alert("나이를 입력해주세요");

document.frm.age.focus();

return false;

}else{

return true;

}

}


결과



댓글

Designed by JB FACTORY