먼저 예제 (텍스트 상자, 암호 상자, 선택, 드롭 다운 양식 등)로 사용자 제출 인터페이스를 사용하면 다음과 같습니다.
<! docType html public "-// w3c // dtd html 4.01 Transitional // en" "http://www.w3.org/tr/html4/loose.dtd"> html> http-equiv ="content-type "contents ="text/html; charset = utf-8 "> <title> 사용자 등록 </title> </witch> </head> <body> <!-사용자 등록-> <form action ="/requesttest/request5 "method ="get "> <table> <!-텍스트 입력 상자-> <tr> <td> username </td> <td> <입력 유형 ="text "="usern "/>>>>>>>>>>>>>/>. box-> <tr> <td> 비밀번호 </td> <td> <입력 유형 = "비밀번호"이름 = "비밀번호"/> </td> </tr> <!-라디오 버튼 라디오-> <tr> <td> gender </td> <td> <입력 유형 = "radio"name = "gender"value = "male"/> input type = "radio" "galend" 여성 </td> </tr> <!-확인란-> <td> hosts </td> <td> <입력 유형 = "checkbox"name = "Hobby"value = "sport"/> sports = "input byp ="checkbox "name ="value "value ="music "/> input type ="checkbox "value ="/>>>>>>>>>>>>>>>>>>>>>>>>>> td> <input byp = "checkbox"name = "Hobby" box-> <tr> <td> city </td> <select name = "city"> <옵션 value = "Beijing"> Beijing </옵션> <옵션 값 = "Shanghai"> Shanghai </옵션 value = "shenzhen"> Shenzhen </옵션> </td> <!-Multi-lline Text Box 프로파일 </td> <td> <textarea rows = "5"cols = "60"name = "access"> </textarea> </td> </tr> <td> <td colspan = "2"> <입력 유형 = "value ="regver "/> </td> </tr> </table> </body </html> </html>
참고 : HTML <form> 태그의 정의 및 사용은 다음과 같습니다.
<!-필수 조치 속성 양식을 제출할 때 양식 데이터를 보낼 위치를 지정합니다. -> <form action = "value">
속성 값은 URL이며 양식 데이터를 보낼 위치를 나타냅니다. 가능한 가치 :
절대 URL- 다른 사이트를 가리 킵니다 (예 : src =”www.example.htm.htm”)
상대 URL- 사이트 내 파일을 가리 킵니다 (예 : src =”example.htm”)
예를 들어, 다음 양식에는 두 개의 입력 필드와 제출 버튼이 있습니다. 양식을 제출할 때 양식 데이터는 "form_action.asp"라는 페이지에 제출됩니다.
<form action = "form_action.asp"method = "get"> <p> 이름 : <input type = "text"name = "fname" /> < /p> <p> 성 : <input type = "text"name = "lname" /> < /p> <입력 유형 = "제출"value = "제출" />
메소드가 얻어 지므로 정보는 서블릿의 doget 메소드에서 검색됩니다.
public class requestservlet5 확장 httpservlet {public void doget (httpservletrequest request, httpservletreponse response) throws servletexception, ioexception {// post bread beed code // request.setcharacterencoding ( "utf-8"); getparameter string usename을 통한 get get get get get get get get get. request.getParameter ( "username"); system.out.println (username); // 값 가져 오기 // get gret bread code (예 : 중국어를 입력) --- 수동 인코딩 사용 // username = urlencoder.encode (username, "iso-8859-1"); // iso // username = urldecoder.decode (username, "utf-8"); // UTF-8로 디코딩 username = new String (username.getBytes ( "iso-8859-1"), "UTF-8"); System.out.println (username); // null verification if (username! = null && username.trim (). longth ()> 0) {system.out.println ( "username legal");} // getParameter를 사용하여 데이터를 제출합니다. 기본적으로 첫 번째 데이터 만 얻을 수 있습니다. 문자열 취미 = request.getParameter ( "취미"); // 객관식 상자 System.out.println (Hobby); // CheckBox에 제출 된 모든 데이터 가져 오기 --- getParameterValuestring [] Hobbies = request.getParameterValues("hobby");System.out.println(Arrays.toString(hobbies));System.out.println("------------------------------------------------------------------------------------------------------------------------------------------- ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- request.getParameterNames (); Arrays.toString(request.getParameterValues(name)));}System.out.println("--------------------------------------------------------------------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ParametErmap.keyset (); for (문자열 키 : 키) {// 키는 매개 변수 NameSystem.out.println (key + ":" + arrays.toString (parameteRmap.get (key));}} public void dopost (httpservletrequest 요청, httpservletexection e, ioepletexcence e, getexcence) 응답);}}매개 변수의 이유
URL 인코딩은 브라우저에서 양식 입력을 패키지하기 위해 사용하는 형식입니다. 브라우저는 양식에서 모든 이름과 값을 가져 오거나 이름/값 매개 변수로 인코딩합니다 (전송할 수없는 문자를 제거하거나 데이터 순위 등을 제거 함).
다른 요청 방법은 다른 솔루션에 해당합니다.
—- request.setcharacterencoding ( "클라이언트 인코딩 세트");
수동으로 변형 코드를 해결하십시오
username = urlencoder.encode (사용자 이름, "iso-8859-1"); // encode username = urldecoder.decode (username, "utf-8"); // UTF-8로 디코딩합니다
위의 쓰기 방법을 단순화하십시오. username = new String (username.getBytes ( "ISO-8859-1"), "UTF-8");
GET BREBLE CONFIGURATION TOMCAT 기본 디코딩 문자 세트를 얻으십시오
tomcat/conf/server.xml에서
커넥터에 속성 uriencoding = "utf-8"을 추가하십시오
결론 : 개발 중에 Tomcat의 기본 디코딩 세트를 수정하지 마십시오. 최대한 많이 사용하여 요청을 제출하십시오. 사용해야하는 경우 수동으로 인코딩하십시오.