---
# System prepended metadata

title: servlet&JSP 作業
tags: [Servlet&JSP]

---

# servlet&JSP 作業
###### tags: `Servlet&JSP`
1.
姓名
電話
性別(男女)
學歷(大學/研究所)
通訊地址
興趣(口運動口閱讀口看電影口其它)
java
```
String title = "作業1";
		  		  
		  String name = request.getParameter("name");
		  String phone = request.getParameter("phone");
		  String gender = request.getParameter("gender");
		  String education = request.getParameter("education");
		  String address = request.getParameter("address");
		  String[] a = request.getParameterValues("a");
		  
		  out.println(			  
				       "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
				       "Transitional//EN\n" +
		               "<HTML>\n" +
		               "<HEAD><TITLE>" + title + "</TITLE></HEAD>\n" +			    
			           "<H1 ALIGN=CENTER>" + title + "</H1>\n" +
			           "<UL>\n" +
			           "  <LI><B>姓名</B>: "
			           + name + "\n" +
			           "  <LI><B>電話</B>: "
			           + phone + "\n" +
			           "  <LI><B>性別</B>: "
		  			   + gender + "\n" +
			           "  <LI><B>學歷</B>: "
			           + education + "\n" +
			           "  <LI><B>通訊地址</B>: "
			           + address + "\n" +
			           "  <LI><B>興趣</B>: ");
			     try {
				   for(int i=0 ; i<a.length ; i++){
					   out.println(a[i]);
				   }
				
			} catch (Exception e) {
				
			}

		  out.println(
			           "</UL>\n" +
			           "</BODY></HTML>");	
	}
```
html
```
<H1 ALIGN="CENTER">作業1</H1>
 <FORM ACTION="./home1"method="post">
  姓名:  <INPUT TYPE="TEXT" NAME="name" ><BR>
  電話: <INPUT TYPE="TEXT" NAME="phone"><BR>
  性別:  <select name="gender" size="1" >
  <option  value="男">男</option>
  <option  value="女">女</option></select><BR>
  學歷:  <select name="education" size="1" > 
  <option  value="大學">大學</option>
  <option  value="研究所">研究所</option></select><BR>
  通訊地址:  <INPUT TYPE="TEXT" NAME="address"><BR>
  <label>興趣:</label>
    <label><INPUT TYPE="checkbox" NAME="a" value="運動">運動</label>
    <label><INPUT TYPE="checkbox" NAME="a" value="閱讀">閱讀</label>
    <label><INPUT TYPE="checkbox" NAME="a" value="看電影">看電影</label>
    <label><INPUT TYPE="checkbox" NAME="a" value="其它">其它</label>
  <CENTER>
```
2.
設計一個HTML程式讀取使用者帳號與密碼
設計一個servlet程式讀取web.xml檔中的密碼值
比較HTML傳送給servlet程式的帳密，若不符合印出"error"訊息

jsp1
```
<body>
	<h1>
		<%=request.getServletContext().getInitParameter("ERROR")%>
	</h1>
	<h1><%=request.getAttribute("name")%></h1>
	<h1><%=request.getAttribute("password")%></h1>
	<form action='home2.html'>
		<input name="button" type="submit" id="button" value="Home"><br>
	</form>
```
jsp2
```
<body BGCOLOR="#9F35FF">
	<h1>
		<%=request.getServletContext().getInitParameter("true")%>
	</h1>
	<h1><%=request.getAttribute("name")%></h1>
	<h1><%=request.getAttribute("password")%></h1>
	<form action='home2.html'>
		<input name="button" type="submit" id="button" value="Home"><br>
	</form>
	<H1 ALIGN="CENTER"><img src="C:\Users\Student\Desktop\11.jpg"></H1>>
</body>
```
java取 init
```@WebServlet(
		urlPatterns = { "/home2" }, 
		initParams = { 
				@WebInitParam(name = "user", value = "sa"), 
				@WebInitParam(name = "password", value = "manager")
		})
ServletConfig sc=this.getServletConfig();
String sa =sc.getInitParameter("user");
String manager =sc.getInitParameter("password"); 
```
補充:java 取xml init
```
ServletConfig sc=this.getServletConfig();
String name1 =sc.getInitParameter("name");
String password1 =sc.getInitParameter("password");


if(name.equals(name1) && password.equals(password1)) //equals要改
```
java equals
```
if(name.equals(sa) && password.equals(manager)) {
			request.setAttribute("name", "帳號:"+name);
			request.setAttribute("password","密碼:"+password);	
			request.getRequestDispatcher("/homework2.jsp").forward(request, response);
		}else{
			request.setAttribute("name", "帳號:"+name);
			request.setAttribute("password", "密碼:"+password);
			request.getRequestDispatcher("/home2.jsp").forward(request, response);
			
		}	
```
html
```
<H1 ALIGN="CENTER">作業2</H1>
 <FORM ACTION="./home2"method="post">
  帳號:  <INPUT TYPE="TEXT" NAME="name" ><BR>
  密碼:  <INPUT TYPE="password" NAME="password"><BR>
     	<input type="submit" value="送出"/>
		
 </FORM>
 ```
xml
 ```
     <servlet>
    <servlet-name>home2</servlet-name>
    <servlet-class>home2</servlet-class>
       <init-param>
         <param-name>name</param-name>
         <param-value>sa</param-value>
      </init-param>
    <init-param>
         <param-name>password</param-name>
           <param-value>manager</param-value>
     </init-param>
    </servlet>
  <servlet-mapping>
  		<servlet-name>home2</servlet-name>
  		<url-pattern>/home2</url-pattern>
  </servlet-mapping>
  <context-param>
	<param-name>ERROR</param-name>
	<param-value>密碼錯誤, 請返回感謝!</param-value>
  </context-param> 
  <context-param>
	<param-name>true</param-name>
	<param-value>恭喜你，密碼正確!</param-value>
  </context-param> 
  ```

# 作業三
![](https://i.imgur.com/QqC6C6Y.png)
![](https://i.imgur.com/poRS4I6.png)
html->servlet->javaBean->jsp
用第一個作業進階版
建一個新的 classmate
把資料全部塞進去 classmate
用request,response
給jsp
沒有參數的建構子多寫一個
![](https://i.imgur.com/2UppvTt.png)

home3page.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>
<H1 ALIGN="CENTER">作業3</H1>
 <FORM ACTION="./home3"method="post">
  姓名:  <INPUT TYPE="TEXT" NAME="name" ><BR>
  電話: <INPUT TYPE="TEXT" NAME="phone" ><BR>
  性別:  <select name="gender" size="1" >
  <option  value="男">男</option>
  <option  value="女">女</option></select><BR>
  學歷:  <select name="education" size="1" > 
  <option  value="大學">大學</option>
  <option  value="研究所">研究所</option></select><BR>
  通訊地址:  <INPUT TYPE="TEXT" NAME="address"><BR>
 
  <label>興趣:</label>
    <label><INPUT TYPE="checkbox" NAME="a" value="運動">運動</label>
    <label><INPUT TYPE="checkbox" NAME="a" value="閱讀">閱讀</label>
    <label><INPUT TYPE="checkbox" NAME="a" value="看電影">看電影</label>
    <label><INPUT TYPE="checkbox" NAME="a" value="其它">其它</label>
   
  <CENTER>
    <INPUT TYPE="SUBMIT" name="submit" value="送出">
  </CENTER>
  </FORM>

</body>
</html>
```
Home3bean.java
```
package home3bean;

public class Home3bean{
	String name;
	int phone;
	String gender;
	String education;
	String address;
	String[] a;
	
	public Home3bean()
	{
		
	}

	public Home3bean(String pName,int pPhone, String pGender, String pEducation, String pAddress ,String[] pA) {
		this.name = pName;
		this.phone = pPhone;
		this.gender = pGender;
		this.education = pEducation;
		this.address = pAddress;
		this.a = pA;
		
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}



	public int getPhone() {
		return phone;
	}

	public void setPhone(int phone) {
		this.phone = phone;
	}

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	public String getEducation() {
		return education;
	}

	public void setEducation(String education) {
		this.education = education;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String[] getA() {
		return a;
	}

	public void setA(String[] a) {
		this.a = a;
	}
	
	

}
```
home3.java
```

import home3bean.*;
import java.io.IOException;
import java.util.List;

import javax.servlet.ServletConfig;
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("/home3")
/**
 * Servlet implementation class home3
 */

public class home3 extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	private static final String CONTENT_TYPE = "text/html; charset=UTF-8";
	private static final String CHARSET_CODE = "UTF-8";


	protected void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException,IOException{
		
		request.setCharacterEncoding(CHARSET_CODE);
		response.setContentType(CONTENT_TYPE);
	
//		  response.setHeader("Cache-Control","no-cache"); 
//		  response.setHeader("Pragma","no-cache"); 
//		  response.setDateHeader ("Expires", -1);
		if(request.getParameter("submit")!=null) 
			gotoSub(request, response);
	
	}
	public void gotoSub(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
		String name;
		int phone;
		String gender;
		String education;
		String address;
		String[] a = null;
		
		
			
		name = request.getParameter("name").trim();
		if(request.getParameter("phone").equals(null)) {
			phone = Integer.parseInt(request.getParameter("phone"));
		}else {
			phone=0;
		}
		gender = request.getParameter("gender").trim();
		education = request.getParameter("education").trim();
		address = request.getParameter("address").trim();
		a = request.getParameterValues("a");
		Home3bean H3b = new Home3bean(name,phone,gender,education,address,a);
		request.getSession(true).setAttribute("H3b",H3b);
		request.getRequestDispatcher("/home3pagin.jsp").forward(request, response);
		
		
	
	}
}
```
home3pagin.jsp
```
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
 
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<jsp:useBean id="H3b" class="home3bean.Home3bean" scope="session"/>
<H1 ALIGN="CENTER">作業3</H1>
<FORM ACTION="./home3"method="post">
<% try{%>
  姓名:  <jsp:getProperty name="H3b" property="name" /><BR>
  電話:  <jsp:getProperty name="H3b" property="phone" /><BR>
  性別:  <jsp:getProperty name="H3b" property="gender" /><BR>
  學歷:  <jsp:getProperty name="H3b" property="education"/> <BR>
  通訊地址:<jsp:getProperty name="H3b" property="address"/><BR> 
  <%
}catch(NumberFormatException e){
	
}
  %>
  興趣:
  <%try{ %>
  <%
	for(int i=0 ; i<H3b.getA().length ; i++ ){
		%>
	<%=H3b.getA()[i] %>
<%
}
%>
  
 <%
  }catch(NullPointerException e){
  }
 %> 


  </FORM>
</body>
</html>
```