所以我正在尝试使用servlet在网络上显示信息而且我被卡住了,我尝试搜索和研究但是徒劳无功,我找不到任何帮助。我想知道这可能是服务器的问题吗?当我尝试用Java中的dopost方法运行我的html时,它一直给我这个错误HTTP Status 404
。否则,当我在我的服务器上运行我的Java类时,它显示没有任何问题,当我在网络上运行我的html时它显示没有任何问题,但CANNOT
获取方法。请任何帮助将不胜感激。我的代码如下.. /我的.JSP
文件也出现了同样的错误。这只是我决定使用的示例,以便用我的主代码解决我的问题。
我的Java代码
package helloServelets;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class LearningServelets
*/
@WebServlet("/LearningServelets")
public class LearningServelets extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String yourname = request.getParameter("YourName");
String enter = request.getParameter("Enter");
String school = request.getParameter("SchoolName");
String work = request.getParameter("Work");
String Home = request.getParameter("home");
response.setContentType("text/html");
PrintWriter output = response.getWriter();
output.println("<html><body><h3>Hello " + yourname);
output.println("</h3><br />" + enter + " + " + school+work);
output.println(Home + "<br /> " + "</body></html>");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
}
我的HTML代码如下
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form method="post" action="LearningServelets">
What is your YourName?
<input name = "Yourname">
<p>
What is your Gender
<input name ="Enter">
</p>
<p>
Where did you go to school?
<input name = "SchoolName">
</p>
<p>
Where do you Work?
<input name = "Work">
</p>
<p>
Where do you live?
<input name = "home">
</p>
<input type ="submit">
</form>
</body>
</html>
我的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>ServeletsLearning</display-name>
<servlet>
<servlet-name>LearningServelets</servlet-name>
<servlet-class>helloServelets.LearningServelets</servlet-class>
</servlet>
</web-app>
答案 0 :(得分:0)
试试这个:
index.html文件
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form method="post" action="LearningServlet">
What is your age?<input name = "age"><br>
What is your Gender?<input name ="gender"><br>
Where did you go to school?<input name = "schoolname"><br>
Where do you Work?<input name = "work"><br>
Where do you live?<input name = "home"><br>
<input type ="submit">
</form>
</body>
LearningServlet.java文件 包装helloservlet;
import java.io.IOException;
import java.io.PrintWriter;
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("/LearningServlet")
public class LearningServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public LearningServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String age=request.getParameter("age");
String gender=request.getParameter("gender");
String schoolname=request.getParameter("schoolName");
String work=request.getParameter("work");
String home=request.getParameter("home");
response.setContentType(getServletContext().getMimeType("text/html"));
PrintWriter pw=response.getWriter();
pw.write("<html><body>");
pw.write("Age :"+age+"<br>");
pw.write("Gender :"+gender+"<br>");
pw.write("School Name :"+schoolname+"<br>");
pw.write("Work :"+work+"<br>");
pw.write("Home :"+home+"<br>");
pw.write("</body></html>");
}
}
的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" version="3.1">
<display-name>helloservlet</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
现在在服务器上部署项目并在浏览器中打开: http://localhost:8080/helloservlet/index.html
注意:假设您的应用程序/项目名称是helloservlet