我试图以乐透模拟器的形式从网页中获取一组数字。我运行服务器,通过浏览器中的本地主机打开页面,输入数字并提交它,但下一页上没有显示任何内容,甚至没有404错误。可能是一个映射问题,但我迷路了。有什么想法吗?
Lotto.class
import java.io.*;
import java.util.Arrays;
import java.util.Comparator;
import javax.servlet.*;
import javax.servlet.http.*;
public class Lotto extends HttpServlet {
public void doGet(HttpServletRequest servletRequest, HttpServletResponse
servletResponse)
throws ServletException, IOException {
servletResponse.setContentType("text/html");
PrintWriter out = servletResponse.getWriter();
String userInput1 = servletRequest.getParameter("number1");
int userNum = Integer.parseInt(userInput1);
String userInput2 = servletRequest.getParameter("number2");
int userNum2 = Integer.parseInt(userInput2);
String userInput3 = servletRequest.getParameter("number3");
int userNum3 = Integer.parseInt(userInput3);
String userInput4 = servletRequest.getParameter("number4");
int userNum4 = Integer.parseInt(userInput4);
String userInput5 = servletRequest.getParameter("number5");
int userNum5 = Integer.parseInt(userInput5);
String userInput6 = servletRequest.getParameter("number6");
int userNum6 = Integer.parseInt(userInput6);
if(
userNum != userNum2 || userNum != userNum3 || userNum != userNum4 ||
userNum != userNum5 || userNum != userNum6 &&
userNum2 != userNum || userNum2 != userNum3 || userNum2 != userNum4 ||
userNum2 != userNum5 || userNum2 != userNum6 &&
userNum3 != userNum || userNum3 != userNum2 || userNum3 != userNum4 ||
userNum3 != userNum5 || userNum != userNum6 &&
userNum4 != userNum || userNum4 != userNum2 || userNum4 != userNum3 ||
userNum4 != userNum5 || userNum5 != userNum6 &&
userNum5 != userNum || userNum5 != userNum2 || userNum5 != userNum3 ||
userNum5 != userNum4 || userNum5 != userNum6 &&
userNum6 != userNum || userNum6 != userNum2 || userNum6 != userNum3 ||
userNum6 != userNum4 || userNum6 != userNum5){
int[] numberArray = {userNum, userNum2,
userNum3,userNum4,userNum5,userNum6,};
out.println("<html><body>Your numbers selection in order, " +
Arrays.toString(numberArray) + " valid </body></html>");
}
else{
out.println("<html><body> Error! </body></html>");
}
}
}
的index.html
<!DOCTYPE html>
<html>
<body>
<h1 > Lotto Servlet Game </h1 >
<h2 > Please choose your numbers below </h2 >
<form action="Lotto">
<fieldset>
Please enter your name: <br><input type="text" name="username"><br>
#1:<br>
<input type="text" name="number1" min="1" max="42">
<br>
#2:<br>
<input type="text" name="number1" min="1" max="42">
<br>
#3:<br>
<input type="text" name="number1" min="1" max="42">
<br>
#4:<br>
<input type="text" name="number1" min="1" max="42">
<br>
#5:<br>
<input type="text" name="number1" min="1" max="42">
<br>
#6:<br>
<input type="text" name="number1" min="1" max="42">
<br><br>
<input type="submit" min="Submit">
</fieldset>
</form>
</body>
</html>
的web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-
app_2_4.xsd" xmlns:xsi="
http://www.w3.org/2001/XMLSchema-
instance" xmlns="
http://java.sun.com/xml/ns/j2ee">
<display-name>Sample Application</display-name>
<description> Sample Application. </description>
<servlet>
<servlet-name>LottoServlet</servlet-name>
<servlet-class>Lotto</servlet-class>
</servlet>
<servlet-mapping>
<servlet- name>LottoServlet</servlet-name>
<url-pattern>/Lotto</url-pattern>
</servlet-mapping>
</web-app>
这三个文件包含以下目录:
Lotto.class Z:\ Apache的Tomcat的8.5.5 \ web应用\样品\ WEB-INF \类\ Lotto.class
的index.html Z:\ Apache的Tomcat的8.5.5 \ web应用\样品\ index.html的
的web.xml Z:\ Apache的Tomcat的8.5.5 \ web应用\样品\ WEB-INF \ web.xml中
答案 0 :(得分:0)
在index.html中,所有字段名称都是number1。
将这些参数传递给servlet时。它将在您调用Integer.parseInt(userInput2)时抛出异常。
答案 1 :(得分:0)
如有疑问,请删除代码。例如,将servlet更改为:
public class Lotto extends HttpServlet {
public void doGet(HttpServletRequest servletRequest,
HttpServletResponse servletResponse)
throws ServletException, IOException {
servletResponse.setContentType("text/html");
PrintWriter out = servletResponse.getWriter();
out.println("<html><body>You are in the servlet</body></html>");
}
}
可以告诉你它正在运作。如果你走得那么远,你可以进一步调试。那时你将了解到你的比较逻辑有点搞砸了。但首先,让事情运转起来。正如@adamM所提到的,你可以尝试点击,例如首先在你的浏览器中点击http://localhost:8080/sample/Lotto。
答案 2 :(得分:0)
您是否检查过.war文件是否具有正确的结构?即如果你&#34;添加框架支持 - &gt;网页&#34;它创建了web.xml,但是如果你看看你的工件结构是不正确的......
使用以下结构构建工件:
name.war
-> META-INF
->MANIFEST.MF
->WEB-INF
->lib
->required .jars if needed
->compile-output (thats the name in IntelliJ)
如果为部署选择了.war文件,请检查运行配置。 也许这有帮助,我在tomcat上部署时遇到了很多问题,因为控制台没有提供.war文件不正确的信息。