Created
December 8, 2015 08:44
-
-
Save Cartman0/608bf31409934b92244f to your computer and use it in GitHub Desktop.
Formタグから取得したデータをHTMLに表示するサンプル。(エスケープ処理なし)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@ page language="java" contentType="text/html; charset=UTF-8" %> | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>requestForm</title> | |
</head> | |
<body> | |
<p>インプットフォーム</p> | |
<p>値を入力してください</p> | |
<%-- GETメソッドでテキストを送信 --%> | |
<form action="./RequestServlet"> | |
<p> | |
ここに値を入れてください:<input type="text" name="text1"> | |
</p> | |
<p> | |
ここに値を入れてください:<input type="text" name="text2"> | |
</p> | |
<input type="submit" value="送信"> | |
</form> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
/** | |
* Formデータ連携 | |
*/ | |
@WebServlet("/RequestServlet") | |
public class RequestServlet extends HttpServlet { | |
private static final long serialVersionUID = 1L; | |
/** | |
* コンストラクタ. | |
*/ | |
public RequestServlet() { | |
super(); | |
} | |
/** | |
* GETメソッドでリクエストされた場合の処理. | |
* @param request | |
* @param response | |
* @throws javax.servlet.ServletException | |
* @throws java.io.IOException | |
*/ | |
@Override | |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | |
String text1 = ""; // テキスト1格納用変数 | |
String text2 = ""; // テキスト2格納用変数 | |
// JSPの画面から値を取得 | |
text1 = request.getParameter("text1"); | |
text2 = request.getParameter("text2"); | |
// 画面に出力する内容の設定 | |
// 出力する内容がHTMLであることを設定 | |
response.setContentType("text/html"); | |
// 出力する画面の文字コードをUTF-8に設定 | |
response.setCharacterEncoding("UTF-8"); | |
// 画面に出力するためのWriterクラスインスタンスを取得 | |
PrintWriter pw = response.getWriter(); | |
// HTMLを出力 | |
pw.println("<html>"); | |
pw.println("<head>"); | |
pw.println("<title>入力結果</title>"); | |
pw.println("</head>"); | |
pw.println("<body>"); | |
pw.println("<h1>入力結果</h1>"); | |
pw.println("<p>入力された値は「" + text1 | |
+ "」と「" + text2 + "」です。</p>"); | |
pw.println("</body>"); | |
pw.println("</html>"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
あああ