dポイントプレゼントキャンペーン実施中!

環境 eclipse tomcat6
javaファイルとweb.xmlを用いており
職業を選択し送信ボタンを押すとあなたの職業はXXXですと表示するはずなのですが
HTTPステータス 404がでて困っています
どこが間違っているのでしょうか
package servlet;
import java.io.*;

import javax.servlet.*;
import javax.servlet.http.*;

public class RadioInput extends HttpServlet{

String html;

String html1="<!doctype html><html lang=ja><head><meta charset=utf-8><title>Radioinput</title></head></body>";

String html2="職業をお選びください<form action=Radioinput method=post><table><tr><td><input type=radio name=occupation value=会社員>会社員<br><input type=radio name=occupation value=学生>学生<br><input type=radio name=occupation value=主婦>主婦<br><input type=radio name=occupation value=その他>その他</td></tr></table><br><input type=submit value =送信><input type=reset value=リセット></form></body></html>";


public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
html=html1+html2;
out.println(html);
out.close();
}

public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
int numberOfErrors=0;
String errorMessage="<div class=alerm>入力値にエラーがありました</div>";
String noRadioValueError="<div class=alerm>「エラー入力値がありません」</div>";


request.setCharacterEncoding("UTF-8");
String occupation=request.getParameter("occupation");

if(occupation==null){
numberOfErrors +=1;
errorMessage +=noRadioValueError;
}

response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();

if(numberOfErrors>0){
html = html1+errorMessage+html2;
out.println(html);
}else{
html=html1+"あなたの職業は"+occupation+"ですね</body></html>";
out.println(html);
}
out.close();
}
}

A 回答 (2件)

## String html2="職業をお選びください<form action=Radioinput method=post> ・・・


の部分ですが、
String html2="職業をお選びください<form action='./RadioInput' method=post>
でどうですか?

あと、大きなお世話かもしれませんけど、メンバー変数の
String html
ですが、勉強レベルなら構いませんがsynchronizedとかしないと複数からリクエストがあった時、
同じ結果が複数のクライアントへ表示されてしまう場合がありますよ。
特に問題がなさそうなので、ローカル変数としてしまうのも手ですね。
    • good
    • 0

web.xmlをできれば載せてください。


URL指定をどのようにしたか補足してください。
(例: http://localhost:8080/Hoge
    • good
    • 0

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!