[Java] POST、history.go(-1) 簡單應用

GET:可以透過網址直接進入網頁
POST:不能透過網址直接進入網頁

history.go(-1):返回上一頁
ex. 
<input type="button" name="btn" value="上一頁" onclick="history.go(-1);" />


一、程式結果
二、程式碼




一、程式結果

Project 類型: Dynamic Web Project
Project 名稱: Servlet
Package 名稱: idv.ycc.servlet
Class 類型: Servlet
Class 名稱: PostServlett.java

postPersonalInfomation.html

PostServlet.java 傳送資料後

PostServlet.java 直接透過網址進入


二、程式碼


postInformation.html
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<style>
.container{
text-align: center;
}
.line{
display: flex;
justify-content: left;
margin: 10px 0;
}
.description{
margin: 0 45px;
}
.submit{
margin: auto;
}
</style>
<title>傳送資料</title>
</head>
<body>
<form action="/Servlet/PostServlet" method="POST">
<div class='container'><br/>
<fieldset style='width: 40%'>
<legend>填寫使用者資料</legend>
<div class='line'>
<div>請填寫您的姓名:</div>
<div>
<input type="text" name="name" class="text" />
</div>
</div>
<div class='line'>
<div>請填寫您的密碼:</div>
<div>
<input type="password" name="password" class="text"/>
</div>
</div>
<div class='line'>
<div>請再次輸入密碼:</div>
<div>
<input type="password" name="passwordConfirm" class="text"/>
</div>
</div>
<div class='line'>
<div>請選擇您的性別:</div>
<div>
<input type="radio" name="sex" value="男" id="sexMale"/>
<label for="sexMale">男</label>
<input type="radio" name="sex" value="女" id="sexFemale"/>
<label for="sexFemale">女</label>
</div>
</div>
<div class='line'>
<div>請輸入您的年齡:</div>
<div>
<input type="text" name="age" class="text"/>
</div>
</div>
<div class='line'>
<div>請選擇您的興趣:</div>
<div>
<input type="checkbox" name="interesting" value="電影音樂" id="i1"/>
<label for='i1'>電影音樂</label>
<input type="checkbox" name="interesting" value="旅遊" id="i2"/>
<label for="i2">旅遊</label>
<input type="checkbox" name="interesting" value="社交" id="i3"/>
<label for="i3">社交</label>
</div>
</div>
<div class='line'>
<div>請選擇出生地區:</div>
<div>
<select name="area">
<option>-----地區-----</option>
<optgroup label="台北市">
<option value="中正區">中正區</option>
<option value="中山區">中山區</option>
</optgroup>
<optgroup label="台南市">
<option value="安平區">安平區</option>
<option value="永康區">永康區</option>
</optgroup>
</select>
</div>
</div>
<div class='line'>
<div>自我介紹:</div>
<div class="description">
<textarea name="description" rows="8">請填寫其他資料</textarea>
</div>
</div>
<div class="line">
<div class="submit">
<input type="submit" name="btn" value="傳送資料">
</div>
</div>
</fieldset>
</div>
</form>
</body>
</html>

PostServlet.java
package idv.ycc.servlet;
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 SearchServlet
 */
@WebServlet("/PostServlet")
public class PostServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public PostServlet() {
        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("BIG5"); //設定response的編碼
response.getWriter().println("請使用 POST 方式提交資料"); //印出於網頁上
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setCharacterEncoding("UTF-8"); //設定response編碼
request.setCharacterEncoding("UTF-8"); //設定request編碼
String name = request.getParameter("name"); //取得form表單的姓名
String password = request.getParameter("password"); //取得form表單的密碼
String sex = request.getParameter("sex"); //取得form表單的性別

int age = 0;
try {
age = Integer.parseInt(request.getParameter("age")); //取得form表單的年齡並從String轉為Integer
}catch (Exception e){
e.printStackTrace();
}
String[] interesting = request.getParameterValues("interesting"); //取得form表單的興趣,因為是多個值同個name所以用陣列
String area = request.getParameter("area"); //取得form表單的年齡

response.setContentType("text/html"); //設定網頁內容為html
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE html>");
out.println("<HTML>");
out.println("<BODY>");
out.println("<div>姓名:" + name + "</div>");
out.println("<div>密碼:" + password + "</div>");
out.println("<div>性別:" + sex + "</div>");
out.println("<div>年齡:" + age + "</div>");
out.println("<div>興趣:");
for(String str : interesting)
out.println(str + ", ");
out.println("</div>");
out.println("<div>出生地:" + area + "</div>");
out.println("<br/><div>");
out.println("<input type=\'button\' name=\'btn\' value=\'上一頁\' onclick=\'history.go(-1);\' />");
out.println("</div><br/>");
out.println("</BODY>");
out.println("</HTML>");
}
}


留言

這個網誌中的熱門文章

[日文] Google日文輸入法 簡單安裝說明

[面試] 日月光 設備工程師

[Windows] 還我 win7 相片檢視器!!