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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19405757/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 16:53:31  来源:igfitidea点击:

How to get data from JSP page to servlet

javajspservletspostservlet-listeners

提问by Prasanna

I'm new to Servlet functionality. I'm trying to get some data in JSP Form and trying to print it in console using Servlet. But i'm not able to do that.

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

web.xml

网页.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>  

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

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);
    }

}

After execution, it throws me the following error,

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

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.

采纳答案by user2821894

@Prassana:Please modify your web.xml as below and it should work. I tested your code and its working for me. This will work for both GET and POST.

@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>

回答by Dark Knight

Need to change action in form tag with following

需要通过以下方式更改表单标签中的操作

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

回答by Hussain Akhtar Wahid 'Ghouri'

replace this : <form action="Startup"

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

by this : <form action="/Startup"

这样 : <form action="/Startup"

回答by Suresh Atta

Change mapping

更改映射

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

Step2 : add ovverideannotation

Step2:添加ovveride注解

  @Override
  public  void doPost(HttpServletReques...

It is unable to detect your post method and trying to hit getmethod I guess.

get我猜它无法检测到您的 post 方法并尝试点击方法。

And try to check with get method also once,

并尝试使用 get 方法检查一次,

@Override
  public void doGet(HttpServletReques...