JSP/サーブレットでTomcatを使ってWebアプリケーションを作ってます。
JSP内のFormタグでサーブレットクラスを呼び出すと404エラーになります。
該当のソースコード
JSP
1<%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3<!DOCTYPE html> 4<html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>test</title> 8 </head> 9 <body> 10 <form action="/examples/Servlet1/WEB-INF/Servlet1" method="post"> 11 12 <input type="text" name="x" size="10">×<input type="text" name="x" size="10">=<input type="submit"> 13 14 15 </body> 16</html>
Java
1import java.io.*; 2import java.io.IOException; 3import javax.servlet.RequestDispatcher; 4import javax.servlet.ServletContext; 5import javax.servlet.ServletException; 6import javax.servlet.annotation.WebServlet; 7import javax.servlet.http.HttpServlet; 8import javax.servlet.http.HttpServletRequest; 9import javax.servlet.http.HttpServletResponse; 10 11import model.Logic1; 12import model.Beans; 13 14@WebServlet("/Servlet1") 15 16public class Servlet1 extends HttpServlet{ 17 private static final long serialVersionUID = 1L; 18 19 20 21 protected void doGet(HttpServletRequest request, 22 HttpServletResponse response) 23 throws ServletException,IOException{ 24 25 26 ServletContext context = request.getServletContext(); 27 RequestDispatcher rd = context.getRequestDispatcher("/top.jsp"); 28 rd.forward(request,response); 29 30 31 //String disp = "/top.jsp"; 32 //RequestDispatcher dispatch = request.getRequestDispatcher(disp); 33 34 //dispatch.forward(request, response); 35 36 } 37 38 protected void doPost(HttpServletRequest request, 39 HttpServletResponse response) 40 throws ServletException,IOException{ 41 42 43 String x = request.getParameter("x"); 44 String y = request.getParameter("y"); 45 46 47 48 Beans be = new Beans(); 49 be.setX (Integer.parseInt(x)); 50 be.setY (Integer.parseInt(y)); 51 52 ; 53 54 Logic1 lg = new Logic1(); 55 lg.execute(be); 56 57 request.setAttribute("be",be); 58 59 60 61 62 String disp = "/result.jsp"; 63 RequestDispatcher dispatch = request.getRequestDispatcher(disp); 64 65 dispatch.forward(request, response); 66 } 67 }
ファイル構成は
[examples]
......+[Servlet1]
...........+[WEB-INF]
..............+[classes]
...................+Servlet1.class
..............+[jsp]
...................+top.jsp
にしました。
試したこと
<form action="/Servlet1" method="post"> <form action="./Servlet1" method="post"> <form action="/Servlet1/Servlet1" method="post"> <form action="./Servlet1/Servlet1" method="post">回答1件
あなたの回答
tips
プレビュー