Main.java(servlet)ではJSTLを使った時用のコードになっていて、main.jspではJSTLを使っていないコードだったため、mian.jspでもcoreタグライブラリを使用しました。
エラーも java.lang.NullPointerExceptionから変わッタノですが、属性名と属性の間にスペースを入れたところ治りました。ありがとうございました。
java
1HTTP Status 500 - /WEB-INF/jsp/main.jsp (line: 24, column: 44) The JSP specification requires that an attribute name is preceded by whitespace 2 3type Exception report 4 5message /WEB-INF/jsp/main.jsp (line: 24, column: 44) The JSP specification requires that an attribute name is preceded by whitespace 6 7description The server encountered an internal error that prevented it from fulfilling this request. 8 9exception 10 11org.apache.jasper.JasperException: /WEB-INF/jsp/main.jsp (line: 24, column: 44) The JSP specification requires that an attribute name is preceded by whitespace 12 org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:42) 13 org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:443) 14 org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:89) 15 org.apache.jasper.compiler.Parser.parseAttributes(Parser.java:164) 16 org.apache.jasper.compiler.Parser.parseAttributes(Parser.java:153) 17 org.apache.jasper.compiler.Parser.parseCustomTag(Parser.java:1225) 18 org.apache.jasper.compiler.Parser.parseElements(Parser.java:1442) 19 org.apache.jasper.compiler.Parser.parse(Parser.java:138) 20 org.apache.jasper.compiler.ParserController.doParse(ParserController.java:242) 21 org.apache.jasper.compiler.ParserController.parse(ParserController.java:102) 22 org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:199) 23 org.apache.jasper.compiler.Compiler.compile(Compiler.java:374) 24 org.apache.jasper.compiler.Compiler.compile(Compiler.java:354) 25 org.apache.jasper.compiler.Compiler.compile(Compiler.java:341) 26 org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:662) 27 org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:364) 28 org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395) 29 org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339) 30 javax.servlet.http.HttpServlet.service(HttpServlet.java:731) 31 org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 32 servlet.Main.doGet(Main.java:54) 33 javax.servlet.http.HttpServlet.service(HttpServlet.java:624) 34 javax.servlet.http.HttpServlet.service(HttpServlet.java:731) 35 org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 36note The full stack trace of the root cause is available in the Apache Tomcat/7.0.65 logs. 37 38Apache Tomcat/7.0.65
javaでsnsを作っているのですが、NullPointerExceptionが出てしまいます。原因がわかりません、アドバイスの方をどうぞよろしくお願いします。
//変更後→nullPointerではなくTTP Status 500 - /WEB-INF/jsp/main.jsp (line: 24, column: 44) The JSP specification requires that an attribute name is preceded by whitespaceこのようなエラーに変わりました。
このコードはSerlvlet の Main.javaです。→スペースを入れたところ治りました。
java
1package servlet; 2 3 4import java.util.List; 5import javax.servlet.RequestDispatcher; 6 7import java.io.IOException; 8import java.sql.DriverManager; 9 10import javax.servlet.ServletException; 11import javax.servlet.annotation.WebServlet; 12import javax.servlet.http.HttpServlet; 13import javax.servlet.http.HttpServletRequest; 14import javax.servlet.http.HttpServletResponse; 15import javax.servlet.http.HttpSession; 16 17import com.mysql.jdbc.Connection; 18import com.mysql.jdbc.PreparedStatement; 19 20import model.GetMutterListLogic; 21import model.Mutter; 22import model.User; 23import model.PostMutterLogic; 24 25/** 26 * Servlet implementation class Main 27 */ 28@WebServlet("/Main") 29public class Main extends HttpServlet { 30 31 private static final long serialVersionUID = 1L; 32 33 /** 34 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 35 */ 36 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 37 38 //つぶやきリストを取得して、リクエストスコープに保存 39 GetMutterListLogic getMutterListLogic=new GetMutterListLogic(); 40 List<Mutter>mutterList=getMutterListLogic.execute(); 41 request.setAttribute("mutterList", mutterList); 42 43 44 45 //ログインしているか確認するため 46 //セッションスコープからユーザー情報を取得 47 HttpSession session=request.getSession(); 48 User loginUser=(User)session.getAttribute("loginUser"); 49 50 if(loginUser==null){//ログインしていない場合 51 //リダイレクト 52 response.sendRedirect("/docoTsubuu/"); 53 }else{//ログイン済みの場合 54 //フォワード 55 RequestDispatcher dispatcher=request.getRequestDispatcher("/WEB-INF/jsp/main.jsp"); 56 dispatcher.forward(request, response); 57 } 58 59 } 60 protected void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{ 61 //リクエストパラメータの取得 62 request.setCharacterEncoding("UTF-8"); 63 String text=request.getParameter("text"); 64 65 //入力値チェック 66 if(text!=null&&text.length()!=0){ 67 68 //セッションスコープに保存されたユーザー情報を取得 69 HttpSession session=request.getSession(); 70 User loginUser=(User)session.getAttribute("loginUser"); 71 72 //つぶやきをつぶやきリストに追加 73 Mutter mutter=new Mutter(loginUser.getName(),text); 74 PostMutterLogic postMutterLogic=new PostMutterLogic(); 75 postMutterLogic.execute(mutter); 76 77 //つぶやきリストを習得して、リクエストスコープに保存 78 GetMutterListLogic getMutterListLogic=new GetMutterListLogic(); 79 List<Mutter> mutterList=getMutterListLogic.execute(); 80 request.setAttribute("mutterList", mutterList); 81 82 83 }else{ 84 //エラーメッセージをリクエストスコープに保存 85 86 request.setAttribute("errorMsg", "つぶやきが入力されていません"); 87 } 88 //メイン画面にフォワード 89 RequestDispatcher dispatcher=request.getRequestDispatcher("/WEB-INF/jsp/main.jsp"); 90 dispatcher.forward(request, response); 91 92 // TODO Auto-generated method stub 93 response.getWriter().append("Served at: ").append(request.getContextPath()); 94 } 95 96} 97
このコードは main.jspです。main.jspもJSTLを使ったコードに変更しました。
java
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 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 5<html> 6<head> 7<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 8<title>どこつぶ</title> 9</head> 10<body> 11<h1>どこ粒メイン</h1> 12<p> 13<c:out value="${loginUser.name}"/>さん、ログイン中 14<a href="/docoTsubuu/Logout">ログアウト</a> 15</p> 16<p><a href="/docoTsubuu/Main">更新</a></p> 17<form action="/docoTsubuu/Main"method="post"> 18<input type="text"name="text"> 19<input type="submit" value="つぶやく"> 20</form> 21<c:if test="${not empty errorMsg}"> 22<p>${errorMsg}</p> 23</c:if> 24<c:forEach var="mutter"items="${mutterList}"> 25<p><c:out value="${mutter.userName}"/>: 26<c:out value="${mutter.text}"/></p> 27</c:forEach> 28</body> 29</html> 30
皆さんどうぞよろしくお願いします。
回答3件
あなたの回答
tips
プレビュー