404 Apache Tomcat with java and html - java

I am having when using apache tomcat with java. The problem might be in the web.xml but I dont know how to fix it. This is my project explorer:
So the thing is that when I run registro.html everything goes as expected:
But when I click on enviar the 404 appears:
So this is my code in registro.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Registro</title>
</head>
<body>
<form action="/Ejer2/Registro" method="POST">
<input type=hidden name=registro value=resultadoRegistro>
<BR><BR>Username: <input type=text name=user>
<BR><BR>Password: <input type=password name=pass>
<BR><BR><input type=submit value="Enviar"><input type=reset>
</form>
</body>
</html>
And this is my Registro.java:
package Ejer2;
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;
import javax.servlet.http.HttpSession;
import javax.servlet.*;
#SuppressWarnings("deprecation")
#WebServlet(urlPatterns="/Registro")
public class Registro extends HttpServlet implements SingleThreadModel{
private static final long serialVersionUID = 1L;
public Registro() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
HttpSession session=req.getSession(true);
Usuario miuser=(Usuario)session.getValue(session.getId());
if(miuser==null){
miuser=new Usuario(req.getParameter("user"),req.getParameter("password"));
session.putValue(session.getId(),miuser);
}
res.setContentType("text/html");
String user=req.getParameter("user");
//String pass = req.getParameter("pass");
PrintWriter toClient = res.getWriter();
toClient.println("<html>");
toClient.println("<title>REGISTRO REALIZADO</title>");
toClient.println("Usuario "+user+" registrado con exito");
toClient.println("</html>");
toClient.close();
}
}
And this my web.xml:
I don't know what to do, thanks in advance.

Related

404 Http Status by servlet

I have created a post yesterday regarding this, but someone decided to close it. I have tried solutions from the post, it didn't work. If answer from the post could solve my issues, I won't be asking the same question again. Tried Put servlet class in a package, Set servlet URL in url-pattern, Use domain-relative URL to reference servlet from HTML. The thing is my registration.jsp is almost the same as my login.jsp same for the servlets as well, I dont get why it doesnt work for registration when it works for the login servlet. I also have clean and restarted my server multiples times. Didnt install tomcat into the computer just import it thru zip files into eclipse.
HTTP Status 404 return by Servlet
Servlet returns "HTTP Status 404 The requested resource (/servlet) is not available"
Login Servlet
package net.login.controller;
import java.io.IOException;
import java.sql.SQLException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.annotation.WebServlet;
import net.login.dao.UserDAO;
import net.login.model.Users;
#WebServlet("/login")
public class UserLoginServlet extends HttpServlet{
public #interface WebServlet {
}
private static final long serialVersionUID = 1L;
public UserLoginServlet() {
super();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
UserDAO userDao = new UserDAO();
try {
Users user = userDao.checkLogin(username, password);
String destPage = "login.jsp";
if (user != null) {
HttpSession session = request.getSession();
session.setAttribute("user", user);
destPage = "home.jsp";
} else {
String message = "Invalid username/password";
request.setAttribute("message", message);
}
RequestDispatcher dispatcher = request.getRequestDispatcher(destPage);
dispatcher.forward(request, response);
} catch (SQLException | ClassNotFoundException ex) {
throw new ServletException(ex);
}
}
}
register.jsp
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Registration</title>
<script src="https://code.jquery.com/jquery-3.4.1.min.js"
integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
crossorigin="anonymous"></script>
<script type="text/javascript"
src="https://cdn.jsdelivr.net/npm/jquery-validation#1.19.0/dist/jquery.validate.min.js"></script>
<link href=”bootstrap/css/bootstrap.min.css” rel=”stylesheet” type=”text/css” />
<script type=”text/javascript” src=”bootstrap/js/bootstrap.min.js”></script>
</head>
<body>
<div style='text-align=center' class="container">
<h1>New User Registration</h1>
<form action='register' method='POST' role='form'>
<div class='registerform'>
<label for='username'> Username: </label>
<input type="text" placeholder='Username' class='form-control' name='username'>
</div>
<div class='registerform'>
<label for='password'> Password: </label>
<input type="password" class='form-control' placeholder='Password' name='password'>
</div>
<br><br>
<button type="submit" class="btn btn-default"> Register </button>
</form>
</div>
</body>
</html>
UserRegisterServlet
package net.login.controller;
import javax.servlet.http.HttpServlet;
import java.io.IOException;
import java.sql.SQLException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import net.login.dao.UserDAO;
import net.login.model.Users;
#WebServlet("/register")
public class UserRegisterServlet extends HttpServlet{
public #interface WebServlet{
}
private static final long serialVersionUID = 1L;
public UserRegisterServlet() {
super();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// String pathInfo = request.getPathInfo();
// String[] pathParts = pathInfo.split("/");
// String part1 = pathParts[1];
// String part2 = pathParts[2];
//
// System.out.println(part1 + part2); //doesnt work for now since cant find /register
String username = request.getParameter("username");
String password = request.getParameter("password");
UserDAO userDAO = new UserDAO();
try {
userDAO.createUsers(username, password);
String destPage = "register.jsp";
System.out.println("Success");
RequestDispatcher dispatcher = request.getRequestDispatcher(destPage);
dispatcher.forward(request, response);
}catch(SQLException | ClassNotFoundException ex) {
throw new ServletException(ex);
}
}
}

Java Apache Tomcat Required resource not found

Im getting an error i dont know why. I have this project:
And when i execute registro.html it works nicely:
But when i submit the post in the code i think it doesnt reach Registro.java
the code in registro.html is:
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Registro</title>
</head>
<body>
<form action="http://localhost:8080/Ejer2/Registro.java" method="POST">
<input type=hidden name=registro value=resultadoRegistro>
<BR><BR>Username: <input type=text name=user>
<BR><BR>Password: <input type=password name=pass>
<BR><BR><input type=submit value="Enviar"><input type=reset>
</form>
</body>
</html>
and in Registro.java this one:
package Ejer2;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.*;
#SuppressWarnings("deprecation")
public class Registro extends HttpServlet implements SingleThreadModel{
private static final long serialVersionUID = 1L;
public Registro() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
HttpSession session=req.getSession(true);
Usuario miuser=(Usuario)session.getValue(session.getId());
if(miuser==null){
miuser=new Usuario(req.getParameter("user"),req.getParameter("password"));
session.putValue(session.getId(),miuser);
}
res.setContentType("text/html");
String user=req.getParameter("user");
//String pass = req.getParameter("pass");
PrintWriter toClient = res.getWriter();
toClient.println("<html>");
toClient.println("<title>REGISTRO REALIZADO</title>");
toClient.println("Usuario "+user+" registrado con exito");
toClient.println("</html>");
toClient.close();
}
}
What is the problem?
You can't use or call like Registro.java directly inside the html action rather you need to map the url pattern for your Registro servlet class as shown below:
#WebServlet(urlPatterns="/Registro")
public class Registro extends HttpServlet implements SingleThreadModel{
//Add your code here
}
Now use the above configured urlPattern as the action in your registro.html, shown below:
<form action="/Registro" method="POST">
Also, the other important point is that you don't need SingleThreadModel for the servlet classes in general (because all the requests will be served using the same servlet instance), but if you are using SingleThreadModel on purpose you can leave it as is.

404 Apache tomcat in a Java project

So this is my project:
Where Registro.java is:
package Ejer2;
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;
import javax.servlet.http.HttpSession;
import javax.servlet.*;
#SuppressWarnings("deprecation")
#WebServlet(urlPatterns="/Registro")
public class Registro extends HttpServlet implements SingleThreadModel{
private static final long serialVersionUID = 1L;
public Registro() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
HttpSession session=req.getSession(true);
Usuario miuser=(Usuario)session.getValue(session.getId());
if(miuser==null){
miuser=new Usuario(req.getParameter("user"),req.getParameter("password"));
session.putValue(session.getId(),miuser);
}
res.setContentType("text/html");
String user=req.getParameter("user");
//String pass = req.getParameter("pass");
PrintWriter toClient = res.getWriter();
toClient.println("<html>");
toClient.println("<title>REGISTRO REALIZADO</title>");
toClient.println("Usuario "+user+" registrado con exito");
toClient.println("</html>");
toClient.close();
}
}
And registro.html is:
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Registro</title>
</head>
<body>
<form action="/Ejer2/Registro" method="POST">
<input type=hidden name=registro value=resultadoRegistro>
<BR><BR>Username: <input type=text name=user>
<BR><BR>Password: <input type=password name=pass>
<BR><BR><input type=submit value="Enviar"><input type=reset>
</form>
</body>
</html>
When I run registro.html everything goes as expected:
But when I enter an username and a password it doesnt work:
4
It seems as if it doesnt find the Registro.java. I have tried changing the action="/Ejer2/Registro" to many other things like just /Registro orthe full http://... but still doesnt work.
This is my web.xml:
What can be the problem?
I guess you are missing servlet mapping in your web.xml. You need to register your servlet in web.xml (open web.xml file and at the bottom change tab to see actual source code not designer) add following code and you should be good to go
<servlet>
<servlet-name>RegistroServlet</servlet-name>
<servlet-class>Ejer2.Registro</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RegistroServlet</servlet-name>
<url-pattern>/Registro</url-pattern>
</servlet-mapping>
I also suggest you to step back and start with basic java before attemping to write web application. You have several newbie issueses with your code:
1) name of packages should start with lower case !
2) also url mapping should be with lower case like this /registro
in your form action change url to match urlmapping. In your case it's
form action="/Registro" ...
Ejer2 is name of package it has nothing to do with url mapping. Hope it helps to resolve your problem

Tomcat error HTTP Status 405 - HTTP method GET is not supported by this URL

This is my jsp page
<html>
<head>
<link rel = "stylesheet" href="main.css"/>
<title>Login Page</title> </head>
<body>
<div class ="RegWrap">
<div class ="Set">
<form name="actionForm" action="Connecter" method ="Get">
<table>
<tr><td>Enter your Username: </td>
<td><input type="text" name="userName"/></td></tr>
<tr><td>Enter your Password: </td>
<td><input type="password" name="password"/></td></tr>
<tr><td colspan="2" align="center"><input type="submit" value="login"> </td></tr>
</table>
</form>
</div>
</div>
</body>
</html>
This is my Connecter class :
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;`
public class Connecter extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String n=request.getParameter("userName");
String p=request.getParameter("password");
if(LoginDao.validate(n, p)){
RequestDispatcher rd=request.getRequestDispatcher("welcom");
rd.forward(request,response);`
}
else {
out.print("Sorry username or password error");
RequestDispatcher rd=request.getRequestDispatcher("Sign up.jsp");
rd.include(request,response);
}
out.close();
}`
This is my Welcome page
import java.io.IOException;`
import java.io.PrintWriter;`
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;`
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class welcom extends HttpServlet {
public class WelcomeServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String n=request.getParameter("userName");
out.print("Welcome "+n);
out.close();
}
}
}
This is my Dao page
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class LoginDao {
public static boolean validate(String userName,String password){
boolean status=false;
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection
("jdbc:mysql://localhost:3306 /autolube","root","mehar");
PreparedStatement ps=con.prepareStatement
("select * from person where userName=?and password=?");
ps.setString(1,userName);
ps.setString(2,password);`ResultSet rs=ps.executeQuery();
status=rs.next();`}catch(Exception e){System.out.println(e);
}
return status;
}
}
In your servlet add the below line. As I commented I think its a problem with web.xml. If you are using servlet 3.0 you can use the below ammotation or map in web.xml.
#WebServlet("/Connecter")
public class Connecter extends HttpServlet
if url-pattern is /Connector then add /Connector in your form tag of index.html.

JSP and Java Servlet

I have some code. It works in JSp but not in Java servlet. I am pasting my code here. Can u plz state the mistake in servlet file?
JSP:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
String rollno=null;
String category=null;
rollno=request.getParameter("rollno");
category=request.getParameter("category");
out.println(rollno+"\n"+category+"\n");
%>
</body>
</html>
Java Servlet :-
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;
public class a extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
PrintWriter out=response.getWriter();
String rollno=null;
String category=null;
rollno=request.getParameter("rollno");
category=request.getParameter("category");
out.println(rollno+"\n"+category+"\n");
}
}
Use doGet method not doPost
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}

Categories

Resources