Servlet 无法获取输入
Servlet not able to get input
我正在尝试创建一个程序,我可以在其中接受输入并使用 servlet 获取数字的平方根。我是初学者,所以我知道的不多。问题是当我尝试我的代码时,它不起作用。这是代码:
MyServletDemo.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.lang.Math;
public class MyServletDemo extends HttpServlet {
public void init() throws ServletException {
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html");
String num2 = request.getParameter("num");
int num3 = Integer.parseInt(num2); //This is where the error
int numSqrt = Math.sqrt(num3);
PrintWriter out = response.getWriter();
out.println("<p> The sqrt is "+numSqrt+"</p>");
}
}
index.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>Get sqrt of num</title>
</head>
<body>
<p>Num to find sqrt of: </p> <input type="text" name="num"/>
<a href="welcome">Click to call Servlet</a>
</body>
</html>
我不太了解xml,我在一个教程中找到了代码:
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>Find-the-sqrt-of-num</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>MyHttpServletDemo</servlet-name>
<servlet-class>MyServletDemo</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyHttpServletDemo</servlet-name>
<url-pattern>/welcome</url-pattern>
</servlet-mapping>
</web-app>
结果是:
First this, then when you hit the link, you get :
This result
您收到 null
错误,因为 request.getParameter("num")
为空。在您的 jsp 页面中,您永远不会向您的 servlet 发送任何值,即:通过编写 <a href="welcome">Click to call Servlet</a>
这将带你到 servlet 但不会发送任何参数。而是像下面那样做:
<form method="get" action="Yourservletpage">
<p>Num to find sqrt of: </p> <input type="text" name="num"/>
<input type="submit" name="submit" value="submit" />
</form>
或者试试这个:
<a href="welcome?num=123">Click to call Servlet</a>
您需要做的是向您的 servlet 发出 POST 请求,而不是 GET。这个想法是,如果您需要将信息传递给您的 servlet,那么您就是 POSTING 信息到您的服务器。
如果您没有在 jsp 中指定方法类型,它默认为 GET 并且 GET 用于仅移动到您网站的不同页面而没有 passing/submitting 信息到您的服务器(服务小程序)
花时间了解 GET 和 POST 请求 servlet 的基本区别。
在你的代码中,在标签之后,你需要用一个<form>
标签将所有标签包裹在里面,并将"method"的属性设置为POST类型。在您的 servlet 中,使用 doPost() 方法代替 doGet()。
您可以从使用 jsp 和 servlet 的简单 POST 请求的 google 中找到许多样本。
我正在尝试创建一个程序,我可以在其中接受输入并使用 servlet 获取数字的平方根。我是初学者,所以我知道的不多。问题是当我尝试我的代码时,它不起作用。这是代码:
MyServletDemo.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.lang.Math;
public class MyServletDemo extends HttpServlet {
public void init() throws ServletException {
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html");
String num2 = request.getParameter("num");
int num3 = Integer.parseInt(num2); //This is where the error
int numSqrt = Math.sqrt(num3);
PrintWriter out = response.getWriter();
out.println("<p> The sqrt is "+numSqrt+"</p>");
}
}
index.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>Get sqrt of num</title>
</head>
<body>
<p>Num to find sqrt of: </p> <input type="text" name="num"/>
<a href="welcome">Click to call Servlet</a>
</body>
</html>
我不太了解xml,我在一个教程中找到了代码: web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>Find-the-sqrt-of-num</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>MyHttpServletDemo</servlet-name>
<servlet-class>MyServletDemo</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyHttpServletDemo</servlet-name>
<url-pattern>/welcome</url-pattern>
</servlet-mapping>
</web-app>
结果是: First this, then when you hit the link, you get :
This result
您收到 null
错误,因为 request.getParameter("num")
为空。在您的 jsp 页面中,您永远不会向您的 servlet 发送任何值,即:通过编写 <a href="welcome">Click to call Servlet</a>
这将带你到 servlet 但不会发送任何参数。而是像下面那样做:
<form method="get" action="Yourservletpage">
<p>Num to find sqrt of: </p> <input type="text" name="num"/>
<input type="submit" name="submit" value="submit" />
</form>
或者试试这个:
<a href="welcome?num=123">Click to call Servlet</a>
您需要做的是向您的 servlet 发出 POST 请求,而不是 GET。这个想法是,如果您需要将信息传递给您的 servlet,那么您就是 POSTING 信息到您的服务器。
如果您没有在 jsp 中指定方法类型,它默认为 GET 并且 GET 用于仅移动到您网站的不同页面而没有 passing/submitting 信息到您的服务器(服务小程序)
花时间了解 GET 和 POST 请求 servlet 的基本区别。
在你的代码中,在标签之后,你需要用一个<form>
标签将所有标签包裹在里面,并将"method"的属性设置为POST类型。在您的 servlet 中,使用 doPost() 方法代替 doGet()。
您可以从使用 jsp 和 servlet 的简单 POST 请求的 google 中找到许多样本。