LoginSignup
0
0

More than 3 years have passed since last update.

【自分用】JSPファイルとサーブレットクラスを使用したブラウザ表示

Last updated at Posted at 2020-10-12

初めてJSPとサーブレットクラスの連結がうまくいったので記録。

【参考リンク】
・Html プルダウンメニューの表示について
(https://murashun.jp/blog/20200128-66.html)
・Html 複数行のテキスト入力フォーム作成
(http://www.htmq.com/html/textarea.shtml)

【JSPファイル】


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>お問い合わせフォーム</title>
</head>
<body>


<%--formを挿入する準備 --%>
<form action="/example/P157_s" method="post">

<%--お名前入力タブ --%>
    名前<input type="text" name="name"><br>

<%--お問合せの種類選択タブ --%>
    <select name="qtype">
      <option value="company">会社について</option>
      <option value="product">製品について</option>
      <option value="support">アフターサポートについて</option>
      </select>

<%--お問い合わせ内容入力タブ --%>
      <p>お問い合わせ内容を入力してください</p>
      <textarea name="body" rows="4" cols="40">
      </textarea><br>

      <%--送信ボタン --%>
    <input type="submit" value="送信">

</form>
</body>
</html>

【サーブレットクラス】


package p157_s;

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;

@WebServlet("/P157_s")
public class P157_s extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");

        String name=request.getParameter("name");
        String slags=request.getParameter("qtype");
        String comment=request.getParameter("body");

        //javaコード
        String errorM="";
        if(name==null||name.length()==0) {errorM+="氏名が入力されていません";}
        if(slags==null||slags.length()==0) {errorM+="お問合せの種類が選択されていません";}
        else if(slags.equals("company")){slags="会社について";}
        else if(slags.equals("product")){slags="商品について";}
        else if(slags.equals("support")){slags="アフターサポートについて";}
        if(comment==null||comment.length()==0) {errorM+="お問い合わせ内容が入力されていません";}

        String msg=("氏名["+name+"]様 お問合せの種類["+slags+"] お問合せ内容["+comment+"]");
        if(errorM.length()!=0) {msg=errorM;}

    response.setContentType("text/html; charset=UTF-8");
        PrintWriter out=response.getWriter();

        out.println("<html>");
        out.println("<head>");
        out.println("<title>お問い合わせフォーム</title>");
        out.println("</head>");

        out.println("<meta.charSet=\"UTF-8\">");
        out.println("<body>");
        out.println("<p>"+msg+"</p>");
        out.println("</body>");
        out.println("</html>");
    }
}

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0