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

标签 java jsp servlets post servlet-listeners

我是 Servlet 功能的新手。我试图在 JSP 表单中获取一些数据并尝试使用 Servlet 在控制台中打印它。但我无法做到这一点。

web.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>

ControlServlet.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.

最佳答案

@Prassana:请如下修改您的 web.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>

关于java - 如何从JSP页面获取数据到servlet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19405757/

相关文章:

java - 获得两列的不同组合

java - 将 JSONObject/JSONArray 转换为字符串数组时出现问题

javascript - 如何在 servlet 中加载外部 Html 文件?

java - getResourceAsStream 上下文初始化

java - Spring + Maven + Intellij +HelloWorld

javascript - 调用 Servlet 并从 JavaScript 和参数调用 Java 代码

jsp - jSTL - forEach,具有 bean 列表作为属性的 bean

java - 关于数据库更新的问题

java - AEM:更改了 servlet 的 url

java - 使用 Java 的 Http 流