使用servlet时出现Netbeans错误

时间:2017-01-19 04:25:29

标签: java servlets

我是servlet编程的新手,也是使用IDE进行servlet编程的新手。我刚安装了Netbeans 8.2用于servlet编程,我在执行servlet程序时遇到以下错误。 这是我的index.html

 <html>
    <head>
        <title>TODO supply a title</title>
         </head>
         <body>
             <h4>Click here to go to <a href="CheckPass">CheckPass Page</a></h4>



    </body>
</html>

这是我的servlet程序

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class CheckPass extends HttpServlet
{
public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException
{
response.setContentType("text/html");
PrintWriter out=response.getWriter();
out.println("<form name='frm' method='post' action='/WT Practical 2/servlet/CheckPass'>");
out.println("UserName<input type='text' name='unm'>");
out.println(" Password<input type='password' name='psw'>");
out.println(" <input type='submit' value='Login'>");
out.println("</form>");
out.close();
}
public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException
{
PrintWriter out=response.getWriter();
String unm = request.getParameter("unm");
String ps=request.getParameter("psw");

if(ps.equals("hello123"))
 out.println("Welcome "+unm);
else
 out.println("invalid entry");
}

}

这是web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <servlet>
        <servlet-name>CheckPass</servlet-name>
        <servlet-class>CheckPass</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>CheckPass</servlet-name>
        <url-pattern>/CheckPass</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file> index.html</welcome-file>
    </welcome-file-list>

</web-app>

这个servlet正在为主题Web技术的大学实践执行,所以我的netbeans的项目名称是WT Practical 2.现在,这是我得到的错误

HTTP Status 404 - /WT%20Practical%202/servlet/CheckPass

type Status report

message /WT%20Practical%202/servlet/CheckPass

description The requested resource is not available.

Apache Tomcat/8.0.27

我怀疑是因为我提供了表单操作属性

的路径
<form name='frm' method='post' action='/WT Practical 2/servlet/CheckPass'>

预计要做的是获取用户名和密码,验证它并根据密码提供适当的输出。请帮我。如果我怀疑是正确的,也请告诉我。另外,我听说在servlet程序中编写html代码并不是一个好习惯。我还想知道如何在index.html文件中编写上述html代码,而这应该与该程序的行为方式相同。感谢。

1 个答案:

答案 0 :(得分:1)

是的,在Servlet中编写HTML代码不是一个好习惯。请阅读此Coding style and recommendations

这是基本代码。创建另一个名为start.html的文件并编写以下代码

<html>
   <body>
     <a href="login.html">Goto Login</a>
   </body>
</html>

并像这样更新您的web.xml文件

<welcome-file-list>
    <welcome-file>start.html</welcome-file>
</welcome-file-list>

创建另一个名为login.html的文件并编写以下代码。

<html>
<body>
    <form action="CheckPass" method="POST">
        <input type="text" name="unm" id="unm"/>
        <input type="password" name="psw" id="psw"/>
        <input type="submit" value="Login"/>
    </form>
</body>

在你的Servlet中,CheckPass类写下代码。

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    String unm = req.getParameter("unm");
    String psw = req.getParameter("psw");

    PrintWriter out = resp.getWriter();

    if (unm.equals("someusername") && ps.equals("hello123")) {
        out.println("Welcome " + unm);
    } else {
        out.println("invalid entry");
    }
    out.close();
}

现在运行此应用程序。浏览器将start.html显示为欢迎页面,当您单击Goto Login超链接时,页面将重定向到login.html

为了更好地理解,请阅读下面的StackOverflow文档

  1. Working with doGet() in Java Servlet
  2. Working with doPost() in Java Servlet
  3. 请注意,这个答案只是一个基本的JSP / HTML和Servlet连接。