gpt4 book ai didi

java - "Head First Servlet and JSP"示例不起作用

转载 作者:行者123 更新时间:2023-12-01 12:27:50 25 4
gpt4 key购买 nike

大家好,我正在读一本 jsp 书“Head First Servlet and JSP”,下一个示例不起作用...

html代码:

<!DOCTYPE html>
<html>
<head>
<meta charset="windows-1255">
<title>Insert title here</title>
</head>
<body>

<h1 style="text-align: center;">Beer Selection Page</h1>
<br><br>

<form method = "POST" action="SelectBeer.do">
Select beer characteristics<p>
Color:
<select name ="color" size="1">
<option value="light">light</option>
<option value="amber">amber</option>
<option value="brown">brown</option>
<option value="dark">dark</option>
</select>
<br><br>

<div style="text-align:center">
<input type = "SUBMIT">
</div>
</form>

</body>
</html>

Web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<servlet>
<servlet-name>Ch3 Beer</servlet-name>
<servlet-class>com.example.web.BeerSelect</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>Ch3 Beer</servlet-name>
<url-pattern>/SelectBeer.do</url-pattern>
</servlet-mapping>
</web-app>

Servlet 代码:

package com.example.web;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;


public class BeerSelect extends HttpServlet {

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("Beer Selection Advice<br>");
String c = request.getParameter("color");
out.println("<br>Got beer color " + c);
}

}

当我运行它时,我得到...“HTTP 状态 405 - 此 URL 不支持 HTTP 方法 GET”...我迫切需要帮助。预先感谢您的任何帮助!

最佳答案

您可以通过向 servlet 添加“doGet”方法来解决此问题。使用与 doPost 相同的方法签名:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws     ServletException, IOException

您编写的代码仅包含 doPost 方法。因此,servlet 将仅响应 HTTP POST 请求。 HTTP 405 响应代码是 Servlet 引擎告诉客户端不支持 HTTP GET 的方式。

关于java - "Head First Servlet and JSP"示例不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26189422/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com