I have a login xhtml page, where the users put in their passwords/usernames
here it is :
<?xml version="1.0" encoding="UTF-8"?>
<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
>
<head>
<title>Login</title>
</head>
<h:body>
<form action="/LDAP/Login" method="post">
<p>You need to log in to access protected information.</p>
<table>
<tr>
<td>User name:</td>
<td><input type="text" name="username" /></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="password" /></td>
</tr>
</table>
<p><input type="submit" value="Login" /></p>
</form>
</h:body>
</html>
but when i press submit the java file is not running im not sure why
heres the java file
Login.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package LDAP;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.naming.*;
import javax.naming.directory.*;
import java.util.Hashtable;
public class Login extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
public Login() {
super();
}
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
final String SUCCESS = "loginSuccess.xhtml";
final String FAILURE = "loginError.xhtml";
String strUrl = "login.xhtml";
String username = request.getParameter("username");
String password = request.getParameter("password");
Hashtable env = new Hashtable(11);
boolean b = false;
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://ldap-ser.port.ac.uk:389");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, username + ",ou=student,ou=lan,o=PORT");
env.put(Context.SECURITY_PRINCIPAL, password);
try {
// Create initial context
DirContext ctx = new InitialDirContext(env);
System.out.println();
// Close the context when we're done
b = true;
ctx.close();
System.out.println();
} catch (NamingException e) {
b = false;
System.out.println();
} finally {
if (b) {
System.out.print("Success");
strUrl = SUCCESS;
} else {
System.out.print("Failure");
strUrl = FAILURE;
}
System.out.println();
}
RequestDispatcher rd = request.getRequestDispatcher(strUrl);
rd.forward(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}
}
Heres my web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>ExamplePackage.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/LoginServlet</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Login</servlet-name>
<servlet-class>LDAP.Login</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Login</servlet-name>
<url-pattern>/Login</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>faces/index.xhtml</welcome-file>
</welcome-file-list>
</web-app>
Related
I've tried all methods available on StackOverflow and the internet, but nothing seems to workout as intended, below's a list of what I've tried doing:
All declared methods in my java files are protected named doPost()
I tried using sendRedirect() method instead of RequestDispatcher()
I also tried using another method and calling it in doPost()
I also tried creating another web project in Eclipse, but that too didn't work
Any help would be appreciated, thanks!
Here is my login.html
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
</head>
<body>
<form action="auth" method="post">
<table>
<tr>
<td>Email ID:</td>
<td><input type="email" name="userEmail"></td>
</tr>
<tr>
<td>Password: </td>
<td><input type="password" name="userPassword"></td>
</tr>
<tr>
<td><input type="submit" value="Login"></td>
</tr>
</table>
</form>
</body>
</html>
authenticate.java
package newAuthentication;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class authenticate extends HttpServlet
{
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String userEmail = request.getParameter("userEmail");
String userPassword = request.getParameter("userPassword");
if (validateUser.checkUser(userEmail, userPassword))
{
HttpSession session = request.getSession();
session.setAttribute("userEmail", userEmail);
RequestDispatcher dispatch = request.getRequestDispatcher("home");
dispatch.forward(request, response);
}
else
{
out.println("Incorrect authentication credentials, please try again!");
RequestDispatcher dispatch = request.getRequestDispatcher("login.html");
dispatch.include(request, response);
}
}
}
validateUser.java
package newAuthentication;
import java.sql.*;
public class validateUser
{
public static boolean checkUser(String userEmail, String userPassword)
{
boolean state = false;
try
{
Class.forName("com.mysql.cj.jdbc.Driver");
Connection connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/wad","root","root");
PreparedStatement fetchData = connect.prepareStatement("select * from studentData where email = ? and password = ?");
fetchData.setString(1, userEmail);
fetchData.setString(2, userPassword);
ResultSet data = fetchData.executeQuery();
state = data.next();
}
catch (Exception err)
{
err.printStackTrace();
}
return state;
}
}
home.java
package newAuthentication;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class home extends HttpServlet
{
protected void doPost(HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException
{
doGet(request, response);
response.setContentType("text/html");
PrintWriter out = response.getWriter();
HttpSession session = request.getSession();
String userEmail = (String)session.getAttribute("userEmail");
out.println("Welcome user " + userEmail);
}
}
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_4_0.xsd" id="WebApp_ID" version="4.0">
<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_4_0.xsd" id="WebApp_ID" version="4.0">
<display-name>newAuth</display-name>
<servlet>
<servlet-name>Login</servlet-name>
<servlet-class>newAuthentication.authenticate</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Login</servlet-name>
<url-pattern>/auth</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Home</servlet-name>
<servlet-class>newAuthentication.home</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Home</servlet-name>
<url-pattern>/home</url-pattern>
</servlet-mapping>
<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>
</web-app>
Why you have this problem in the first place:
This chunk of code is MUCH TOO LONG to try all in one step. Especially if you don't have experience working with servlets. If you are not yet sure if your servlets and basic mappings work, start small - a single servlet, a single GET mapping - that you can try in the browser, without any HTML. Just two files in the entire project: web.xml and MyServlet.java - and just leave the doGet method empty.
Right now, your code is so long, that you simply cannot guess the problem once you hit it - there's too many places to verify!
The magic formula is: "try just one thing at a time".
Solution:
Remove the doGet(request, response) call in your doPost method.
You don not provide your own doGet method, so it defaults to what's in the superclass. It so happens, that the default doGet method throws an exception stating that the GET method is not supported. Since you call it from your doPost, the propagating exception makes container think (rightly so) that it's your doPost that is throwing, and so that it is POST that is unsupported.
Also, the call makes no sense.
BTW: Notice that you would find this error in 5 seconds, if you first tried with the suggested two class approach: you would only have this one thing to verify.
I have written a sample code to pass form data from a html page to a servlet. Here are my source codes,
StudentForm.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>HTML Form</title>
</head>Title
<body>
<form action="FormDataServlet" method="get">
FName:<input type="text" name="fName"> <br/>
LName:<input type="text" name="lName"><br/>
<input type="submit" value="submit">
</form>
</body>
</html>
FormDataServlet
package com.kasun.student.form;
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 java.io.IOException;
import java.io.PrintWriter;
/**
* Created by kausn on 5/24/17.
*/
#WebServlet(name = "/FormDataServlet")
public class FormDataServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter printWriter=response.getWriter();
printWriter.print("<html><head></head><body>");
printWriter.print("The F name of the student is "+request.getParameter("fName"));
printWriter.print("The L name of the student is "+request.getParameter("lName"));
printWriter.print("</body></html>");
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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"
version="3.1">
<servlet>
<servlet-name>ServletDemo1</servlet-name>
<servlet-class>com.kasun.servlet.demo.ServletDemo1</servlet-class>
</servlet>
<servlet>
<servlet-name>FormDataServlet</servlet-name>
<servlet-class>com.kasun.student.form.FormDataServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletDemo1</servlet-name>
<url-pattern>/ServletDemo1</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>FormDataServlet</servlet-name>
<url-pattern>/FormDataServlet</url-pattern>
</servlet-mapping>
</web-app>
But when I submit the form it says 404 error that is because it is calling the servlet that exists in http://localhost:63342/ServletDemo/web/FormDataServlet?fName=sfddfddfd&lName=dfdf this path. I know for sure that this is where the bug is, but how to fix this? (Please find the screenshot of package structure as below)
Hello I am trying to create a simple servlet as follows
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Form extends HttpServlet
{
public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
{
res.setContentType("text/html");
PrintWriter p=res.getWriter();
p.println("<html><head></head><body bgcolor=\"red\">The request came from"+req.getMethod()+"</body></html>");
}
}
The req.getMethod() should return POST but is giving me a null value.
I am taking the request from an html file coded as follows.
<html>
<body>
<form action="http://localhost:8080/Form" method="GET">
First Name: <input type="text" name="name"/>
<br>
<input type="submit" value="Submit form "/>
</form>
</body>
</html>
here is the web.xml file. Should I make any changes here.
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0" metadata-complete="true">
<display-name>Welcome to Tomcat</display-name>
<description>
Welcome to Tomcat
</description>
<servlet>
<servlet-name>Form</servlet-name>
<servlet-class>Form</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Form</servlet-name>
<url-pattern>/Form</url-pattern>
</servlet-mapping>
</web-app>
You can write this annotation to attach your servlet to your form:
#WebServlet("/Form")
public class Form extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ... }
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ... }
}
You will have to import javax.servlet.annotation.WebServlet.
This is login.jsp page
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<form action="checklogin" method="Post">
<h2>Login tab</h2> <br>
Login id: <input type="text" name="loginid"><br><br>
Password: <input type="password" name="password"><br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
This is checklogin servlet
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
PrintWriter out=response.getWriter();
javax.servlet.http.HttpSession session = request.getSession(true);
String username = (String)request.getParameter("loginid");
String password = (String)request.getParameter("password");
session.setAttribute("UserName", username);
if(username.equals("Prashant") && password.equals("123"))
{
response.sendRedirect("admin.jsp");
}
else
{
out.println("<h2>"+"Sharam aani chahiye account banaya nahi aur login kar rahe ho"+"<h2>");
}
}
This is my filter which is mapped in filter in we.xml as this URL /admin.jsp/*
public class adminfilter implements javax.servlet.Filter{
#Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("Sawagat nahi karoge init method ka");
}
#Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
System.out.println("Sawagat nahi karoge doFilter method ka");
}
#Override
public void destroy() {
System.out.println("Sawagat nahi karoge destroy method ka");
}
}
This is my 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">
<filter>
<filter-name>adminfilter</filter-name>
<filter-class>Filters_Demo.adminfilter</filter-class>
</filter>
<filter-mapping>
<filter-name>adminfilter</filter-name>
<url-pattern>/admin.jsp/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>checklogin</servlet-name>
<servlet-class>Filters_Demo.checklogin</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>checklogin</servlet-name>
<url-pattern>/checklogin</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
init() and destroy() method are running but doFilter method is not running?
In your doFilter method you should call the next filter in the chain:
chain.doFilter(request, response);
Without this your request will not be processed further after your filter.
Also,I suggest make change in web.xml file
<url-pattern>*.jsp</url-pattern>
This question already has answers here:
Servlet returns "HTTP Status 404 The requested resource (/servlet) is not available"
(19 answers)
Closed 6 years ago.
I am new to servlet and making my first servlet using eclipse.I have made Index.html, Login.java and WelcomeServlet.java. But whenever I am trying to access the using
localhost:8080/ServletExample/
It shows 404 error.Here are the codes..
Index.html
<form action="Login" method="post">
Name:<input type="text" name="userName"/><br/>
Password:<input type="password" name="userPass"/><br/>
<input type="submit" value="login"/>
</form>
Login.java
public class Login extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String n=request.getParameter("userName");
String p=request.getParameter("userPass");
if(p.equals("servlet")) {
RequestDispatcher rd=request.getRequestDispatcher("WelcomeServlet");
rd.forward(request, response);
} else {
out.print("Sorry UserName or Password Error!");
RequestDispatcher rd=request.getRequestDispatcher("/index.html");
rd.include(request, response);
}
}
}
WelcomeServlet.java
package java.io;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class WelcomeServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String n=request.getParameter("userName");
out.print("Welcome "+n);
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<servlet>
<servlet-name>Login</servlet-name>
<servlet-class>Login</servlet-class>
</servlet>
<servlet>
<servlet-name>WelcomeServlet</servlet-name>
<servlet-class>WelcomeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Login</servlet-name>
<url-pattern>/Login</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>WelcomeServlet</servlet-name>
<url-pattern>/WelcomeServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
package java.io;
why u put this line in WelcomeServlet.java.
You're mapping to 'WelcomeServlet' not 'ServletExample'.
Try going to localhost:8080/WelcomeServlet
EDIT: There shouldn't be a trailing slash, sorry!
Make sure ur projrct name is ServletExample.
localhost:8080/ServletExample/index.html