I'm new to servlet programming as well as using IDE's for servlet programming. I just installed Netbeans 8.2 for servlet programming and I am getting the following error while executing my servlet program. This is my 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>
This is my servlet program
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");
}
}
This is web.xml file
<?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>
This servlet is being performed for a college practical of the subject Web Technology so my project name for netbeans is WT Practical 2. Now, this is the error i get
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
I suspect it is because of the line where i gave the path to the form action attribute
<form name='frm' method='post' action='/WT Practical 2/servlet/CheckPass'>
What it's expected to do is take user name and password, verify it and give appropriate output depending upon the password. Please help me. Also let me know if my doubt is correct. Also, I've heard that it's not a good practice to write html code in servlet program. I would also like to know how to write the above html code in index.html file instead which should act the same way this program is expected to act. Thanks.
Yes, it's not a good practice to write HTML code in Servlet. Please read this Coding style and recommendations
Here is the basic code. Create another file called start.html
and write the below code
<html>
<body>
<a href="login.html">Goto Login</a>
</body>
</html>
And update your web.xml
file like this
<welcome-file-list>
<welcome-file>start.html</welcome-file>
</welcome-file-list>
Create another file called login.html
and write the below code.
<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>
In your Servlet ie, CheckPass
class write below code.
@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();
}
Now run this application. The browser displays start.html
as welcome page and when you click on Goto Login hyper link the page redirects to login.html
For better understanding read the below StackOverflow Documentation
Note that this answer is only a basic JSP/HTML and Servlet connection.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.