有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何从JSP页面到servlet获取数据

我不熟悉Servlet功能。我试图以JSP形式获取一些数据,并试图使用Servlet在控制台中打印这些数据。但我做不到

网络。xml

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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">

  <servlet>
    <servlet-name>controlServlet</servlet-name>
    <servlet-class>com.selenium8x8.servlet.ControlServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>controlServlet</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>
</web-app>  

启动。jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
 <form action="Startup" method="post">
        <input type="text" name="name"/><br>        
        <input type="text" name="group"/>
        <input type="text" name="pass"/>
        <input type="submit" value="submit">            
    </form>

</body>
</html>

控制servlet。java

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ControlServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name = request.getParameter("name");
        String group = request.getParameter("group");
        String pass = request.getParameter("pass");
        System.out.println("Name :"+ name);
        System.out.println("group :"+ group);
        System.out.println("pass :"+ pass);
    }

}

在执行之后,它向我抛出以下错误

HTTP Status 405 - HTTP method GET is not supported by this URL

type Status report

message HTTP method GET is not supported by this URL

description The specified HTTP method is not allowed for the requested resource.

共 (3) 个答案

  1. # 1 楼答案

    更改映射

    <form action="/Startup" method="post">
    

    步骤2:添加ovveride注释

      @Override
      public  void doPost(HttpServletReques...
    

    它无法检测到你的post方法并试图点击get方法

    试着用get方法检查一次

    @Override
      public void doGet(HttpServletReques...
    
  2. # 2 楼答案

    替换 这个:<form action="Startup"

    通过这个:<form action="/Startup"

  3. # 3 楼答案

    @Prassana:请修改你的网站。xml如下所示,它应该可以工作。我测试了你的代码,它对我有用。这对GET和POST都有效

    <servlet>
    <servlet-name>ControlServlet</servlet-name>
    <servlet-class>com.selenium8x8.servlet.ControlServlet</servlet-class>
      </servlet>
    
      <servlet-mapping>
    <servlet-name>ControlServlet</servlet-name>
    <url-pattern>/Startup</url-pattern>
      </servlet-mapping>
    </web-app>