前提・実現したいこと
JSPのリンクからサーブレットを呼び出したいです。
隠しパラメータを使用してボタンと同じように実装してみた以下のコードでは
buttonIdがnullになっているようで、リンクをクリックすると
500エラーでNullPointerExceptionが出ます。
どうすればサーブレットを正しく呼び出すことができるか教えていただきたいです。
よろしくお願いします。
JSP
1<%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 4<%@ page import="entity.LoginUser" %> 5<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 6<html> 7<head> 8<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 9<title>どこつぶ</title> 10</head> 11<body> 12<h1>どこつぶログイン</h1> 13 14<c:if test="${sessionScope.loginUser == null}"> 15<a href="/top.jsp">トップへ</a> 16</c:if> 17 18<form action="/doko" method="POST"> 19<c:if test="${sessionScope.loginUser != null}"> 20ようこそ<c:out value="${sessionScope.loginUser.userName}"/>さん 21 22<br> 23<%--この部分 --%> 24<input type="hidden" value="" name="buttonId"> 25<a href onclick="javascript:this.form.buttonId.value='UC0004'; this.form.submit();">つぶやきの投稿・閲覧へ</a> 26</c:if> 27</form> 28</body> 29</html>
Servlet
1@WebServlet("/doko") 2public class Controller extends HttpServlet { 3 private static final long serialVersionUID = 1L; 4 5 protected void doGet(HttpServletRequest request, HttpServletResponse response) 6 throws ServletException, IOException { 7 doPost(request, response); 8 } 9 10 /** 11 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 12 */ 13 protected void doPost(HttpServletRequest request, HttpServletResponse response) 14 throws ServletException, IOException { 15 16 //受け取った値の文字コード設定 17 request.setCharacterEncoding("UTF-8"); 18 19 String page = null; 20 21 String buttonId = request.getParameter("buttonId"); 22 23 //ボタンIDで遷移先の振り分け 24 switch (buttonId) { 25 26 case "UC0001": 27 LoginAction loginAction = new LoginAction(); 28 page = loginAction.execute(request); 29 break; 30 31 case "UC0002": 32 TweetAction tweetAction = new TweetAction(); 33 page = tweetAction.execute(request); 34 break; 35 36 case "UC0003": 37 LogoutAction logoutAction = new LogoutAction(); 38 page = logoutAction.execute(request); 39 break; 40 41 case "UC0004": 42 GoMainAction goMainAction = new GoMainAction(); 43 page = goMainAction.execute(request); 44 break; 45 46 } 47 48 //フォワード 49 RequestDispatcher dispacher = request.getRequestDispatcher(page); 50 dispacher.forward(request, response); 51 52 } 53 54}
Action
1public class GoMainAction { 2 3 public String execute(HttpServletRequest req) { 4 5 //遷移先 6 String page = "/WEB-INF/jsp/main.jsp"; 7 8 //セッション取得 9 HttpSession session = req.getSession(false); 10 11 //ログインチェック。セッションなければnullなのでエラー返す 12 if(session==null){ 13 page = "/dokotsubu/top.jsp"; 14 } 15 16 //セッションから過去のツイートを取り出し 17 ArrayList<String> tweetList = (ArrayList<String>)session.getAttribute("tweet"); 18 19 //過去のツイートある場合 20 session.setAttribute("tweetList", tweetList); 21 22 return page; 23} 24} 25
エラー
HTTPステータス 500 - type 例外レポート メッセージ 説明 The server encountered an internal error that prevented it from fulfilling this request. 例外 java.lang.NullPointerException controller.Controller.doPost(Controller.java:43) controller.Controller.doGet(Controller.java:26) javax.servlet.http.HttpServlet.service(HttpServlet.java:622) javax.servlet.http.HttpServlet.service(HttpServlet.java:729) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
43行目はswitchのbuttonIdです。
回答2件
あなたの回答
tips
プレビュー