質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
JSP

JSP(Java Server Pages)とは、ウェブアプリケーションの表示レイヤーに使われるサーバーサイドの技術のことです。

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

servlet

Servletとは、Webページの動的な生成やデータ処理などをサーバ上で実行するために、Javaで作成されたプログラムです。 ショッピングサイトやオンラインバンキングといった、動的なウェブサイトの構築に用いられています。

Q&A

解決済

1回答

6831閲覧

画面遷移先でパラメータを取得し、処理を分けたい

n000n00

総合スコア25

JSP

JSP(Java Server Pages)とは、ウェブアプリケーションの表示レイヤーに使われるサーバーサイドの技術のことです。

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

servlet

Servletとは、Webページの動的な生成やデータ処理などをサーバ上で実行するために、Javaで作成されたプログラムです。 ショッピングサイトやオンラインバンキングといった、動的なウェブサイトの構築に用いられています。

0グッド

0クリップ

投稿2016/12/29 12:51

編集2016/12/29 12:52

servletで受け取った値で処理を分岐させようとしていますが、
入力チェックの処理を書いたところExceptionが発生します。

未入力で登録もしくは変更を押した場合に、
処理を最初からにしたいのですが、if (error || error2) の error にfalseがセットされていると思うのですが、elseに処理が移らない状況です。

恐れ入りますが、ご教授いただけないでしょうか。
よろしくお願いします。

例外:
javax.servlet.ServletException: java.lang.NumberFormatException: For input string: ""
websample.StartServlet.doPost(StartServlet.java:106)

java

1package websample; 2 3import java.io.IOException; 4import java.sql.SQLException; 5import java.util.ArrayList; 6import java.util.List; 7 8import javax.servlet.ServletException; 9import javax.servlet.http.HttpServlet; 10import javax.servlet.http.HttpServletRequest; 11import javax.servlet.http.HttpServletResponse; 12import javax.servlet.http.HttpSession; 13 14public class StartServlet extends HttpServlet { 15 AccountDAO dao = new AccountDAO(); 16 List<Account> list = new ArrayList<Account>(); 17 18 @Override 19 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 20 response.setContentType("text/plain; charset=UTF-8"); 21 try { 22 dao = new AccountDAO(); 23 list = dao.findAll(); 24 request.setAttribute("list", list); 25 request.getRequestDispatcher("/websample/select.jsp").forward(request, response); 26 } catch (Exception e) { 27 throw new ServletException(e); 28 } 29 } 30 31 @Override 32 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 33 34 String Search = request.getParameter("検索"); 35 String Registration = request.getParameter("登録"); 36 String update = request.getParameter("変更"); 37 String delete = request.getParameter("削除"); 38 39 String id = null; 40 String name = null; 41 String money1 = null; 42 String money2 = null; 43 String keyword = request.getParameter("keyword"); 44 String message = null; 45 boolean error = false; 46 boolean error2 = false; 47 String errorMessage = null; 48 49 HttpSession session = request.getSession(true); 50 51 id = (String) session.getAttribute("ID"); 52 name = request.getParameter("keyword1"); 53 money1 = request.getParameter("keyword2"); 54 money2 = request.getParameter("keyword3"); 55 56 System.out.println("54 ID=" + id); 57 System.out.println("55 name=" + name); 58 System.out.println("56 money1=" + money1); 59 System.out.println("57 money2=" + money2); 60 61 System.out.println("59 Search=" + Search); 62 System.out.println("60 Registration=" + Registration); 63 System.out.println("61 update=" + update); 64 System.out.println("62 delete=" + delete); 65 66 response.setContentType("text/plain; charset=UTF-8"); 67 68 if (id == null || id.length() == 0 || name == null || name.length() == 0 || money1 == null 69 || money1.length() == 0 || money2 == null || money2.length() == 0) { 70 error = false; 71 } 72 73 if (Search == null && Registration == null && update == null && delete == null) { 74 try { 75 list = dao.findAll(); 76 request.setAttribute("list", list); 77 request.getRequestDispatcher("/websample/select.jsp").forward(request, response); 78 } catch (SQLException e) { 79 throw new ServletException(e); 80 } 81 } else if (Search != null || Registration != null || update != null || delete != null) { 82 error2 = true; 83 } 84 85 System.out.println("80 error1=" + error); 86 System.out.println("81 error2=" + error2); 87 88 if (error || error2) { 89 if (Search != null && Search.equals("検索")) { 90 try { 91 list = dao.whereSelect(keyword); 92 message = list.size() + "件ヒット!"; 93 request.setAttribute("list", list); 94 request.setAttribute("message", message); 95 request.getRequestDispatcher("/websample/select.jsp").forward(request, response); 96 } catch (Exception e) { 97 throw new ServletException(e); 98 } 99 } else if (Registration != null && Registration.equals("登録")) { 100 try { 101 int cnt = dao.findNew(name, money1, money2); 102 message = cnt + "件登録しました"; 103 request.setAttribute("message", message); 104 request.getRequestDispatcher("/websample/select.jsp").forward(request, response); 105 } catch (Exception e) { 106 throw new ServletException(e); 107 } 108 } else if (update != null && update.equals("変更")) { 109 try { 110 int cnt = dao.findUpDate(name, money1, money2, id); 111 message = cnt + "件変更しました"; 112 request.setAttribute("message", message); 113 request.getRequestDispatcher("/websample/select.jsp").forward(request, response); 114 } catch (Exception e) { 115 throw new ServletException(e); 116 } 117 } 118 } else if (delete != null && delete.equals("削除")) { 119 try { 120 int cnt = dao.findDelete(id); 121 message = cnt + "件削除しました"; 122 request.setAttribute("message", message); 123 request.getRequestDispatcher("/websample/select.jsp").forward(request, response); 124 } catch (Exception e) { 125 throw new ServletException(e); 126 } 127 128 } else { 129 try { 130 errorMessage = "入力漏れがあるよ"; 131 System.out.println("124 servlet error=" + errorMessage); 132 request.setAttribute("error", errorMessage); 133 request.getRequestDispatcher("/websample/select.jsp").forward(request, response); 134 } catch (Exception e) { 135 throw new ServletException(e); 136 } 137 } 138 } 139}

jsp

1<%@page contentType="text/html; charset=UTF8"%> 2<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 3<html> 4<head> 5<title>accountDB</title> 6<link rel="stylesheet" href="select.css"> 7</head> 8 <body> 9 <c:if test="${message != null}"> 10 <p>処理結果:${message}</p> 11 </c:if> 12 <c:if test="${error != null}"> 13 <p>${error}</p> 14 </c:if> 15 <form name="f" method="POST" action="../DBweb/account"> 16 <input type="text" name="keyword" value="${keyword}"> 17 <input type="submit" name="検索" value="検索"> 18 <input type="button" name="新規" value="新規" onClick="document.location='websample/select_input.jsp?新規=新規';"> 19 20 <c:if test="${!empty list}"> 21 <table border="1"> 22 <tr> 23 <th>ID</th> 24 <th>NAME</th> 25 <th>MONEY</th> 26 <th>MONEY2</th> 27 </tr> 28 <c:forEach var="account" items="${list}"> 29 <tr> 30 <td><a href="websample/select_input.jsp?変更=変更&ID=${account.id}">${account.id}</a></td> 31 <td>${account.name }</td> 32 <td>${account.money }</td> 33 <td>${account.money2 }</td> 34 </tr> 35 </c:forEach> 36 </table> 37 </c:if> 38 </form> 39 </body> 40</html>

jsp

1<%@page contentType="text/html; charset=UTF-8"%> 2<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 3<html> 4<head> 5 <title>入力画面</title> 6 <link rel="stylesheet" href="select.css"> 7</head> 8<body> 9 <% String chenge = request.getParameter("変更"); %> 10 <% String x = request.getParameter("新規"); %> 11 <% String id = request.getParameter("ID"); %> 12 <% String error = (String) request.getAttribute("error"); %> 13 <% System.out.println("chenge=" + chenge); %> 14 <% System.out.println("x=" + x); %> 15 <% System.out.println("error=" + error); %> 16 <% session.setAttribute("ID", id); %> 17 18 <% if (error != null || x == null && chenge.equals("変更")){ %> 19 <p>変更</p> 20 <form method="POST" action="../account"> 21 <div> 22 <label for="name">ID</label> 23 ${ID} 24 </div> 25 <div> 26 <label for="name">NAME</label> 27 <input type="text" name="keyword1"><br> 28 </div> 29 <div> 30 <label for="money">MONEY1</label> 31 <input type="text" name="keyword2"><br> 32 </div> 33 <div> 34 <label for="money">MONEY2</label> 35 <input type="text" name="keyword3"><br> 36 </div> 37 <input type="submit" name="変更" value="変更" onClick="return confirm('変更しますか?')"> 38 <input type="submit" name="削除" value="削除" onClick="return confirm('削除しますか?')"> 39 <input type="submit" name="戻る" value="戻る" > 40 </form> 41 <% } else if (chenge == null && x.equals("新規")){ %> 42 <p>登録</p> 43 <form method="POST" action="../account"> 44 <div> 45 <label for="name">NAME</label> 46 <input type="text" name="keyword1"><br> 47 </div> 48 <div> 49 <label for="money">MONEY1</label> 50 <input type="text" name="keyword2"><br> 51 </div> 52 <div> 53 <label for="money">MONEY2</label> 54 <input type="text" name="keyword3"><br> 55 </div> 56 <input type="submit" name="登録" value="登録" onClick="return confirm('登録しますか?')"> 57 <input type="submit" name="戻る" value="戻る" > 58 </form> 59 <% } %> 60</body> 61</html>

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

申し訳ございません。
論理演算子の考え方に間違えがあったようです。

ありがとうございました。

投稿2016/12/29 14:06

n000n00

総合スコア25

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問