【问题标题】:Servlet not able to get inputServlet 无法获取输入
【发布时间】:2020-04-26 15:05:57
【问题描述】:

我正在尝试创建一个程序,在该程序中我可以接受输入并使用 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

【问题讨论】:

  • 你需要一个表单和一个 POST。

标签: java xml jsp servlets


【解决方案1】:

您需要做的是向您的 servlet 发出 POST 请求,而不是 GET。这个想法是,如果您需要将信息传递给您的 servlet,您就是将信息发布到您的服务器。

如果您没有在 jsp 中指定方法类型,则默认为 GET,而 GET 仅用于移动到您网站的不同页面,而不向您的服务器(servlet)传递/提交信息

花点时间了解 Servlet 的 GET 和 POST 请求的基本区别。

在您的代码中,在标签之后,您需要用&lt;form&gt; 标签将所有标签包装在里面,并将“方法”的属性设置为 POST 类型。在您的 servlet 中,使用 doPost() 方法代替 doGet()。

您可以从 google 找到许多使用 jsp 和 servlet 的简单 POST 请求示例。

【讨论】:

  • 如何将请求值赋予GET?当我尝试将代码放入 POST 时,它会引发错误。
  • 这次你做了哪些改变?更改后您看到了什么不同的错误?
【解决方案2】:

或者试试这个:

<a href="welcome?num=123">Click to call Servlet</a>

【讨论】:

  • 在 servlet 中写什么?
  • 就此而言,我应该用什么来替换“123”以使其具有交互性?
  • 这是你index.jsp中的链接
  • 这是什么意思?
【解决方案3】:

您收到null 错误,因为request.getParameter("num") 为空。在您的jsp 页面中,您永远不会向您的servlet 发送任何值,即:通过编写&lt;a href="welcome"&gt;Click to call Servlet&lt;/a&gt; 这将带您到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>

【讨论】:

  • 我应该将“Yourservletpage”投入使用,还是投入“MyServletDemo”?
  • 您需要输入您的 servlet 名称,即MyServletDemo
  • 我在JSP页面中放了什么?
  • 仅以上代码和 servlet 将保持不变,无需进行任何更改。
  • 我放了代码,但它给出了这个错误:源服务器没有找到目标资源的当前表示或不愿意透露存在。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-19
  • 2020-12-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多