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

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

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

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

Q&A

解決済

1回答

1902閲覧

sendRedirectがうまくいかず困っています

Attsu

総合スコア16

Java

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

0グッド

0クリップ

投稿2017/10/22 06:09

秀和システムのspringFrameWork4 プログラミング入門のP201、図4-23のフォームを送信すると、その内容がMyBeanに追加されるところで404になってしまい手が止まっています。ご教授いただけると幸いです。

###発生している問題・エラーメッセージ

エラーメッセージ

package jp.tuyano.spring.websample1;

import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**

  • Servlet implementation class MySampleServlet

*/
public class MySampleServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

ApplicationContext app; /** * @see Servlet#init(ServletConfig) */ public void init(ServletConfig config) throws ServletException { super.init(); app =new ClassPathXmlApplicationContext("/spring/application-config.xml"); } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { MyBean mybean1 =(MyBean)app.getBean("mybean1"); request.setAttribute("mybean", mybean1); request.getRequestDispatcher("sample").forward(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String message =request.getParameter("message"); MyBean mybean1 =(MyBean)app.getBean("mybean1"); mybean1.addMessage(message); response.sendRedirect("/index.jsp"); }

}

<!DOCTYPE html>

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>

<html> <head> <meta charset="utf-8"> <title>Welcome</title> </head> <body> <h1>Sample Page</h1> <pre>${mybean}</pre> <hr> <form action="sample" method="post"> <input type="text" id="message" name="message"> <input type="submit" value="add"> </form> </body> </html> ```

Web.xmlの記述

<?xml version="1.0" encoding="ISO-8859-1"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>MySampleWebApp1</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/application-config.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <description></description> <display-name>MySampleServlet</display-name> <servlet-name>MySampleServlet</servlet-name> <servlet-class>jp.tuyano.spring.websample1.MySampleServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>MySampleServlet</servlet-name> <url-pattern>/Sample</url-pattern> </servlet-mapping> </web-app>

このように本に記載されているコードに忠実に習ったのですがsendRedirectがうまくいきません。

###補足情報(言語/FW/ツール等のバージョンなど)
より詳細な情報

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

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

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

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

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

guest

回答1

0

ベストアンサー

書籍が手元にないので詳細はわかりませんが、sendRedirect("/index.jsp") と指定されていますので、Webアプリケーション直下に index.jsp が正しく配置されていないからではないでしょうか。

なお、書籍に関する質問はたいてい書籍の巻末に質問先があるかと思いますので、そちらも問い合わせしたほうが良いでしょう。

投稿2017/11/10 00:26

A-pZ

総合スコア12011

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

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

Attsu

2017/11/28 15:58

url-patternの設定の大文字小文字の違いでした。初めての投稿で粗い質問でしたがご回答いただき感謝しています。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問