第一次编写javaweb ,每次运行之后都是显示Http 404,请大佬帮忙指出问题

Servlet代码:
package com.lab;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(name = "/HoroscopeServlet",urlPatterns = "/HoroscopeServlet" )
public class HoroscopeServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String username = request.getParameter("name");
		int a = Integer.parseInt(request.getParameter("sex"));
		int b = Integer.parseInt(request.getParameter("Zodiac"));
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<html><head>");
		out.println("<script type=\"text/javascript\">");
		out.println("function displayHoroscope(g, s){");
		out.println("if(g==0){");
		out.println("if((g+s)<=6) alert(\"You will have a long life.\");");
		out.println("else alert(\"You will have a rich life.\");}");
		out.println("if(g==1){");
		out.println("if((g+s)<=6) alert(\"You will find a tall handsome stranger.\");");
		out.println("else alert(\"You will have six children.\");}}");
		out.println("</script></head>");
		out.println("<body onload=\"displayHoroscope("+a+", "+b+")\">");
		out.println("</body></html>");
}
}
web.xml里的配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>KKK</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>servlet/HoroscopeServlet</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>HoroscopeServlet.html</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>HoroscopeServlet</servlet-name>
 	<servlet-class>HoroscopeServlet</servlet-class>
   </servlet>
   <servlet-mapping>
	<servlet-name>HoroscopeServlet</servlet-name>
	<url-pattern>horoscopeservlet"</url-pattern>
   </servlet-mapping>
</web-app>

html代码
<html>
 <head>
  <title>lab3</title>
  	<script type = “text/javascript">
	function validate(){
		if (document.form[0].elements[0].value=="")
			{
				alert("please enter your name");
				return false;
			}
		if (/^[a-zA-Z]+$/.test(document.forms[0].elements[0].value.charAt(0))==false)
			{
				alert("your name is wrong");
				return false;
			}
		if(/[A-Z]/.test(document.forms[0].elements[0].value.charAt(0))==false)
			{
				alert("your name must start with a captial letter");
				return false;
			}
		var bFlag = false;
		var gender = document.getElementsByName("sex");
		for ( var i = 0; i <= gender.length; i++)
			{ 
				if(gender[i].checked)
				{ 
					bFlag = true;
					break;
				}
			}
		if(bFlag == false)
			{
				alert("please choose your sex");
				return false;
			}
		return true;
		}
	</script>
</head> <body>
  <form action = “/HoroscopeServlet"  method = "get" onsubmit="return validate()">
  	Input your name :
  	<input type="text" name="name">
  	<br><br>
  	Input your sex :
  	<input type="radio" name="sex" value = "0">Male
  	<input type="radio" name="sex" value = "1">Female
  	<br><br>
  	Select your Zodiac sign:
	<select name="Zodiac">
	<option value="0">Aries</option>
	<option value="1">Taurus</option>
	<option value="2">Gemini</option>
	<option value="3">Cancer</option>
	<option value="4">Leo</option>
	<option value="5">Virgo</option>
	<option value="6">Libra</option>
	<option value="7">Scorpio</option>
	<option value="8">Sagittarius</option>
	<option value="9">Capricorn</option>
	<option value="10">Aquarius</option>
	<option value="11">Pisces</option>
	</select>
	<br><br>
	<input type="submit" value="Submit">
	<input type="reset" value="Reset">
</form> 

</html>

查看全部
weixin_44717291
PSG.LGD 咚咚咚
2020/11/25 17:44
  • html5
  • javascript
  • eclipse
  • 点赞
  • 收藏
  • 回答
    私信

1个回复