Servlet 程序未 运行 正确
Servlet program not running properly
我正在 运行 开发一个小的 servlet 程序,但它没有给出预期的输出。
Servletfile.java
public class Servletfile extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/ html");
PrintWriter out= response.getWriter();
String abc = request.getParameter("name");
out.print("name="+abc);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<Form action= "welcome" method="get">
Enter Name :<input type = "text" name="name"><br>
<input type = "Submit" value= "login">
</Form>
</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_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>Practice</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>
<description></description>
<display-name>Servletfile</display-name>
<servlet-name>Servletfile</servlet-name>
<servlet-class>code.Servletfile</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Servletfile</servlet-name>
<url-pattern>/Servletfile</url-pattern>
</servlet-mapping>
</web-app>
在执行代码时,它会显示 index.html 页面,但如果我输入任何名称作为输入,则会出现 404 页面未找到错误。如果我单独 运行 servlet 程序,它会给出 name=null 作为输出。你能给我一些建议吗?
调用操作使用 <%=request.getContextPath()%>/Servletfile
有时容器无法找到您的 servlet,这就是为什么他向您显示 404
。
您需要将 index.html
文件更改为以下内容:
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<Form action= "Servletfile" method="get">
Enter Name :<input type = "text" name="name"><br>
<input type = "Submit" value= "login">
</Form>
</body>
</html>
现在表单将映射到您的 Servletfile Servlet
。当您直接导航到 <a href="http://localhost:8080/Servletfile" rel="nofollow">http://localhost:8080/Servletfile</a>
时看到 null
的原因是您从未设置请求参数 name
.
我正在 运行 开发一个小的 servlet 程序,但它没有给出预期的输出。
Servletfile.java
public class Servletfile extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/ html");
PrintWriter out= response.getWriter();
String abc = request.getParameter("name");
out.print("name="+abc);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<Form action= "welcome" method="get">
Enter Name :<input type = "text" name="name"><br>
<input type = "Submit" value= "login">
</Form>
</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_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>Practice</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>
<description></description>
<display-name>Servletfile</display-name>
<servlet-name>Servletfile</servlet-name>
<servlet-class>code.Servletfile</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Servletfile</servlet-name>
<url-pattern>/Servletfile</url-pattern>
</servlet-mapping>
</web-app>
在执行代码时,它会显示 index.html 页面,但如果我输入任何名称作为输入,则会出现 404 页面未找到错误。如果我单独 运行 servlet 程序,它会给出 name=null 作为输出。你能给我一些建议吗?
调用操作使用 <%=request.getContextPath()%>/Servletfile
有时容器无法找到您的 servlet,这就是为什么他向您显示 404
。
您需要将 index.html
文件更改为以下内容:
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<Form action= "Servletfile" method="get">
Enter Name :<input type = "text" name="name"><br>
<input type = "Submit" value= "login">
</Form>
</body>
</html>
现在表单将映射到您的 Servletfile Servlet
。当您直接导航到 <a href="http://localhost:8080/Servletfile" rel="nofollow">http://localhost:8080/Servletfile</a>
时看到 null
的原因是您从未设置请求参数 name
.