---
# System prepended metadata

title: homeWork2
tags: [Servlet]

---

# homeWork2
###### tags: `Servlet`
java
```
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
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 HomeWork2
 */
@WebServlet("/HomeWork2")
public class HomeWork2 extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public HomeWork2() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
//		String user = request.getParameter("user");
//		String password = request.getParameter("password");
		out.println("<!DOCTYPE HTML>");
		out.println("<html>");
		out.println("<body>");
		
		out.println(bodyContent(request.getReader()));
		out.println("</body></html>");
		
	}
	private String bodyContent(BufferedReader reader) throws IOException{
		String input = null;
		StringBuilder requestBody = new StringBuilder();
		while ((input = reader.readLine())!=null) {
			requestBody.append(input)
			           .append("\n");
		}
		return requestBody.toString();
		
	}

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

}
```

html

```
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Home Work 2</title>
</head>
<body>
<FORM ACTION="./HomeWork2" method="post">
使用者帳號:<INPUT TYPE="TEXT" NAME="user"><BR>
使用者密碼:<INPUT TYPE="password" NAME="password"><BR>


 <CENTER>
    <INPUT TYPE="SUBMIT">
  </CENTER>
  </FORM>


</body>
</html>
```

解法(去解碼)
```
private String bodyContent(BufferedReader reader) throws IOException{
		
		String input = null;
		StringBuilder requestBody = new StringBuilder();
		while ((input = reader.readLine())!=null) {
			requestBody.append(input)
			           .append("\n");
		}
		String text = java.net.URLDecoder.decode(requestBody.toString(), "UTF-8"); //解碼
		return text;
		
	}
```

```


import java.io.IOException;
import java.io.PrintWriter;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * Servlet implementation class HomeWork22
 */
@WebServlet(urlPatterns = {"/HomeWork22"}, 
initParams = { 
//	    @WebInitParam(name = "name", value = "David"), 
//	    @WebInitParam(name = "password", value = "a123456")
	  })
public class HomeWork22 extends HttpServlet {
	private static final long serialVersionUID = 1L;

       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public HomeWork22() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8"); 
		response.setContentType("text/html");   
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		String name = request.getParameter("name");
		String password= request.getParameter("password");
		
		String user=getServletConfig().getInitParameter("name");
		String pswd=getServletConfig().getInitParameter("password");
//		  PrintWriter writer = response.getWriter();
		if (name.equals(user)&&password.equals(pswd)) {
			out.println(name+"Hi !");
		}else {
			out.println("Error !!! ");
		}


	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		doGet(request, response);
	}



}
```
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_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>mywebdemo1</display-name>
  <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>
  <servlet>
    <servlet-name>Homework22</servlet-name>
    <servlet-class>Homework22</servlet-class>  
    <init-param>
  		<param-name>name</param-name>
  		<param-value>David</param-value>
  	</init-param>
  	
  	<init-param>
  		<param-name>password</param-name>
  		<param-value>a123456</param-value>
  	</init-param>  	
  </servlet>
  
   <servlet-mapping>
  		<servlet-name>Homework22</servlet-name>
  		<url-pattern>/Homework22</url-pattern>
  </servlet-mapping>
  <context-param>
	<param-name>ERROR</param-name>
	<param-value>ERROR!!! 再輸入一次!</param-value>
  </context-param> 

</web-app>
```
html
```
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Home Work 22</title>
</head>
<body>

<FORM ACTION="./HomeWork22" method="post">
使用者帳號:<INPUT TYPE="TEXT" NAME="name" id="name"><BR>
使用者密碼:<INPUT TYPE="password" NAME="password" id="password"><BR>
<CENTER>
<INPUT TYPE="SUBMIT">
</CENTER>
</FORM>

</body>
</html>
```