doFilter method is not running? - java

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>

Related

Requested resource not available in Servlet, Tomcat7

I know this question has been asked a number of times and answered as well. However, I am unable to solve the problem. I have tried every possible way of mapping in web.xml. Used annotation #WebServlet also. Still I can't go to my servlet after submitting the html form. Tried changing server location as well. Kindly help.
Please find my web.xml, login page and servlet.
<?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_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>Webservice</display-name>
<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>controller.ItemServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/ItemServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>html/Login.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>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Home Page</title>
<!-- <link rel="stylesheet" TYPE="text/css" href="../css/mystyle.css" ></link>
<link href='http://fonts.googleapis.com/css?family=Lily+Script+One' rel='stylesheet' type='text/css'>
<script type="text/javascript" src="../js/Validations.js"></script>-->
</head>
<body>
<div id='header'>Online Music Store</div>
<div id='content'>
<form name="UserLogin" action="./ItemServlet?req=Login" method="post">
User Id <input type="text" name="userId" ></input>
Password<input type="password" name="password" ></input>
<input type="submit" value="Login"></input>
</form>
</div>
</body>
</html>
/**
* Servlet implementation class ItemServlet
*/
//#WebServlet("/ItemServlet")
public class ItemServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* #see HttpServlet#HttpServlet()
*/
public ItemServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* #see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//response.getWriter().append("Served at: ").append(request.getContextPath());
doPost(request,response);
}
/**
* #see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
CDDao daoObj= new CDDao();
if("Login".equalsIgnoreCase(request.getParameter("req")))
{
System.out.println("Inside if");
System.out.println(request.getParameter("req"));
Change your web.xml like this.
<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>packagename.ItemServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/ItemServlet</url-pattern>
</servlet-mapping>
And your servlet class as
#WebServlet(name="MyServlet", urlPatterns={"/ItemServlet"})
public class ItemServlet extends HttpServlet {
Restart the server and check if there is some problem.
Also, change this line to
<form name="UserLogin" action="ItemServlet" method="post">
Problem is in this line. I believe this is your Login.html which is in html folder. When you use ./ it is a relative url which is looking for ItemServlet in location html/ItemServlet
<form name="UserLogin" action="./ItemServlet?req=Login" method="post">
I do not know your full folder structure but probably this should work for you.
<form name="UserLogin" action="../ItemServlet?req=Login" method="post">
Or provide full context
<form name="UserLogin" action="/[YOUR WEB CONTEXT]/ItemServlet?req=Login" method="post">

Asynchronous Servlets or Filters don't work

I'm new to Java EE and was about to learn to use asynchronous Servlets. I created a web application with a simple index.jsp from which the servlet is called after pressing a button. I always get the following exception:
java.lang.IllegalStateException: Request is within the scope of a
filter or servlet that does not support asynchronous operations
But I set async-supported to true as annotation in the servlet or in web.xml or in both ways. I was searching for hours through similar questions here but I couldn't find a solution. I'm using NetBeans 8.0.2 and glassfish server 4.1 that comes along with NetBeans. Here is my index.jsp:
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Async Test</h1>
<form method="get" action="Asynctest">
<input type="submit"
value="Start test">
</form>
</body>
</html>
the Asynctest servlet:
package Controller;
import java.io.IOException;
import javax.servlet.AsyncContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
#WebServlet(asyncSupported = true, name = "Asynctest", urlPatterns = {"/Asynctest"})
public class Asynctest extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
AsyncContext ac = request.startAsync(request, response);
}
#Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
#Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
}
and the 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>Asynctest</servlet-name>
<servlet-class>Controller.Asynctest</servlet-class>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>Asynctest</servlet-name>
<url-pattern>/Asynctest</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>
It makes me crazy.. Thanks for your help!

Java servets request.getMethod() not working

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.

Java file is not running after xhtml page is submitted

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>

I can't redirect to another jsp page, it reuturns blank page

I have home.jsp page and contact page , what concerns us is home.jsp page as follow:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%# page contentType="text/html;charset=windows-1252"%>
<%# page import="javax.faces.context.FacesContext" %>
<%# taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<f:view>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252"/>
<title>home</title>
</head>
<body>
<%
response.sendRedirect("contact.jsp");
%>
</body>
</html>
</f:view>
the web.xml file code:
<?xml version = '1.0' encoding = 'windows-1252'?>
<web-app 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_2_5.xsd" version="2.5" xmlns="http://java.sun.com/xml/ns/javaee">
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/home.jsp</url-pattern>
</servlet-mapping>
</web-app>
when I load home.jsp it return blank page instead of contact.jsp
First at all, you don't really want to have this code in any page never
<%
response.sendRedirect("something.jsp");
%>
If you want to redirect to "contact.jsp" when accessing to "home.jsp", you should do it in a Filter.
public class MyAppFilter implements Filter {
public void init(FilterConfig filterConfig) throws ServletException {
}
public void destroy() {
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain filterChain) throws IOException, ServletException {
if ((request instanceof HttpServletRequest) && (response instanceof HttpServletResponse)) {
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
HttpServletResponse httpServletResponse = (HttpServletResponse) respo
String requestPath = httpServletRequest.getRequestURI();
//check if the URL contains <anything>/home.jsp
if (requestPath.contains("home.jsp")) {
//if that's the case, redirect to another page
httpServletResponse.sendRedirect("/SportABAWeb/jsf/Esquema.jsf");
return;
}
}
filterChain.doFilter(request, response);
}
}
You should configure the Filter in your web application. Assuming you use a Java EE 6 web application server (like GlassFish 3.x) or Java EE 6 servlet container (like Tomcat 7), you can add this tag to your class to make it a filter
#WebFilter(filterName = "MyAppFilter", urlPatterns = {"/*"})
public class MyAppFilter implements Filter {
//filter implementation...
}
If not, then you should configure it in your web.xml
<filter>
<filter-name>MyAppFilter</filter-name>
<filter-class>my.package.MyAppFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>MyAppFilter</filter-name>
<servlet-name>*.jsp</servlet-name>
</filter-mapping>

Categories