I have a servlet, a filter and a login.jsp. I fill the username and pasword and then click the login button and the filter isn't called, only the servlet is called. The servlet and filter are in different packages and I see this is the problem. If I put servlet and filter in the same package the filter is called successfully. But I want to use them in different packages. What should I do? Thanks in advance!
Filter:
package log.reg.myfilter;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
#WebFilter("/LoginFilter")
public class LoginFilter implements Filter {
public void destroy() {
// TODO Auto-generated method stub
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
PrintWriter out = response.getWriter();
HttpServletRequest req = (HttpServletRequest) request;
String username = req.getParameter("username");
String pass = req.getParameter("password1");
System.out.println(username);
if ((username.length() > 6) && (pass.length() > 3)) {
System.out.println("In filter");
chain.doFilter(request, response);
}
else
out.println("Invalid Input");
}
public void init(FilterConfig fConfig) throws ServletException {
// TODO Auto-generated method stub
}
}
Servlet:
package log.reg;
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;
#WebServlet("/firstServlet")
public class FirstServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("In servlet");
PrintWriter out = response.getWriter();
String username = request.getParameter("username");
out.println("Welcome " + username);
}
}
login.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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>Insert title here</title>
</head>
<body>
<form action="firstServlet" method="get">
<table style="background-color: lightgreen; margin-left: 20px; margin-top: 20px">
<tr>
<td>
<h3 style="color: red">Login Page!!!</h3>
</td>
</tr>
<tr>
<td>UserName : </td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>Password : </td>
<td><input type="password" name="password1"></td>
</tr>
<tr>
<td><input type="submit" name="submit" value="login"></td>
</tr>
</table>
</form>
</body>
</html>
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_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>Servlet ex29 - filter</display-name>
<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>
The value() of #WebServlet and #WebFilter don't have the same meaning.
The first one specifies the URL patterns of the servlet (that is its URL mapping) while the second one specifies the URL patterns to which the filter applies.
But in both cases you specified as name the simple name of the underlying class.
For the servlet, #WebServlet("/firstServlet") makes sense but annotating your Filter with #WebFilter("/LoginFilter") is not what you are looking for as
you don't want to filter only the "/LoginFilter" URL.
For example to filter any requested URL you could specify :
#WebFilter("/*")
A Servlet filter is an object that can intercept HTTP requests targeted at your web application. As I see here, you have not associated filter with servlet. You have to tell the url pattern you want to intercept/filter.
Note: Filter won't be executed independently it works with
Servlet. So You have to associated Filter with Servlets.
If you have to associate with FirstServlet then use below one.
#WebFilter("/firstServlet")
If you want to filter each request you can use /*
#WebFilter("/*")
I'm making my first java web application following a tutorial on youtube. It is a Dynamic Web Application and the code I've written so far is fairly simple but I'm having an issue with HTTP 404 error.
My web.xml appears as followed:`
<?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>PuzzleProject</display-name>
<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>
<servlet>
<description> </description>
<display-name>MyServlet</display-name>
<servlet-name>MyServlet</servlet-name>
<servlet-class>BackEnd</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/MyServlet</url-pattern>
</servlet-mapping>
</web-app>
`
My Index.jsp:
`<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>Welcome, please enter a bunch of words.</h1>
<input type="text" name="words" value="">
<form action = "MyServlet">
<input type="submit" value="Send" />
</form>
</body>
</html>
MyServlet.java:
package BackEnd;
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;
/**
* Servlet implementation class MyServlet
*/
public class MyServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* Default constructor.
*/
public MyServlet() {
// 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());
PrintWriter print = response.getWriter();
String string = "My Name Is Steven";
print.println(string);
}
/**
* #see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
`
Can someone please tell me what I'm doing wrong?
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I keep getting HTTP method POST is not supported by this URL when calling my httpservlet through an html form. I can't see where I'm going wrong. I'm running this on a Tomcat server. Thanks in advance.
The servlet is supposed to print a set of random numbers to the browser.
Here is my servlet:
package servlets;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.TreeSet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import utilities.RandNumSet;
public class RandomServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
public RandomServlet() {
// TODO Auto-generated constructor stub
}
#Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doPost(req, resp);
}
#Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
PrintWriter out = resp.getWriter();
resp.setContentType("text/html");
TreeSet<Integer> randNum = RandNumSet.generateRandNumSet();
Iterator<Integer> iterator = randNum.iterator();
String numString = "";
while (iterator.hasNext()){
numString = numString + iterator.next() + " ";
}
out.println("<head>");
out.println("<title>");
out.println("Your Random Numbers!");
out.println("</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1> Your Random Numbers! </h1>");
out.println("<h1> "+ numString + " </h1>");
out.println("</body>");
}
}
Here is the calling html form snippet:
<div id ="form">
<form action="RandomServlet" method="post">
<input type="submit" value="Randomize!"/>
</form>
</div>
Here are the web.xml snippets
<servlet>
<display-name>RandomServlet</display-name>
<servlet-name>RandomServlet</servlet-name>
<servlet-class>servlets.RandomServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RandomServlet</servlet-name>
<url-pattern>/RandomServlet</url-pattern>
</servlet-mapping>
I am using the following way and its works...
first: check the directory first you have made like-
com.ServletExample
|-JavaResource
|-src
|-com.servletExample
|-RandomServlet.java
|-WebContent
|-META-INF
|-WEB-INF
|-Lib
|-web.xml
|-index.jsp
second: index.jsp is here
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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>Insert title here</title>
</head>
<body>
<div id ="form">
<form action="RandomServlet" method="post">
<input type="submit" value="Randomize!"/>
</form>
</div>
</body>
</html>
third: RendomServlet.java is here
package com.servletExample;
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 RandomServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public RandomServlet() {
super();
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
response.setContentType("text/html");
out.println("<head>");
out.println("<title>");
out.println("Your Random Numbers!");
out.println("</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1> Your Random Numbers! </h1>");
out.println("<h1> " + "Hello" + " </h1>");
out.println("</body>");
}
}
fourth: web.xml is here
<?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"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>JSPExample</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<display-name>RandomServlet</display-name>
<servlet-name>RandomServlet</servlet-name>
<servlet-class>com.servletExample.RandomServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RandomServlet</servlet-name>
<url-pattern>/RandomServlet</url-pattern>
</servlet-mapping>
</web-app>
I hope this will work.
I am trying to add or insert values into table of database using servlet and jsp and MySQL Workbench as database. These are the following details:
1.> Register.java
package register.com;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletConfig;
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.sql.*;
import javax.servlet.*;
/**
* Servlet implementation class Register
*/
#WebServlet("/register")
public class Register extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* #see HttpServlet#HttpServlet()
*/
public Register() {
super();
// TODO Auto-generated constructor stub
}
/**
* #see Servlet#init(ServletConfig)
*/
public void init(ServletConfig config) throws ServletException {
// TODO Auto-generated method stub
}
/**
* #see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
*/
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
/**
* #see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
/**
* #see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
//String connectionURL = "jdbc:mysql://127.0.0.1:3306/newData";// newData is the database
//Connection connection;
Connection conn=null;
String url="jdbc:mysql://localhost:3306/";
String dbName="userlogindb";
String driver="com.mysql.jdbc.Driver";
//String dbUserName="root";
//String dbPassword="root";
try{
String Fname = request.getParameter("fname");
String Mname = request.getParameter("mname");
String Lname = request.getParameter("lname");
String Uname = request.getParameter("username");
String Emailid = request.getParameter("emailid");
String Mobno = request.getParameter("mobno");
String Address = request.getParameter("address");
String Password1 = request.getParameter("password1");
String Password2 = request.getParameter("password2");
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url+dbName,"root", "root");
PreparedStatement pst =(PreparedStatement) conn.prepareStatement("insert into 'userlogindb'.'registerutable'(fname,mname,lname,username,emailid,mobno,address,password1,password2) values(?,?,?,?,?,?,?,?,?)");//try2 is the name of the table
pst.setString(1,Fname);
pst.setString(2,Mname);
pst.setString(3,Lname);
pst.setString(4,Uname);
pst.setString(5,Emailid);
pst.setString(6,Mobno);
pst.setString(7,Address);
pst.setString(8,Password1);
pst.setString(9,Password2);
int i = pst.executeUpdate();
conn.commit();
String msg=" ";
if(i!=0){
msg="Record has been inserted";
pw.println("<font size='6' color=blue>" + msg + "</font>");
}
else{
msg="failed to insert the data";
pw.println("<font size='6' color=blue>" + msg + "</font>");
}
pst.close();
}
catch (Exception e){
pw.println(e);
}
}
}
2.> index.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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>Insert title here</title>
</head>
<body>
<form name="registrationform" action="register" method="post">
<p>
Enter your first name: <input type="text" name="fname"><br>
Enter your middle name: <input type="text" name="mname"><br>
Enter your last name: <input type="text" name="lname"><br>
</p><br>
<p>
Enter username: <input type="text" name="username"><br>
</p><br>
<p>
Enter email id: <input type="text" name="emailid"><br>
Enter mobile number: <input type="text" name="mobno"><br>
Enter address: <textarea rows="5" cols="5" name="address"></textarea><br>
</p><br>
<p>
Enter the password: <input type="password" name="password1"><br>
Reenter the password: <input type="password" name="password2"><br>
</p><br>
<p>
<input type="submit">
</p><br>
</form>
</body>
</html>
3.> web.xml
<?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>RegisterExample</display-name>
<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>
<servlet>
<description>Register Servlet</description>
<display-name>Register</display-name>
<servlet-name>Register</servlet-name>
<servlet-class>register.com.Register</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Register</servlet-name>
<url-pattern>/register</url-pattern>
</servlet-mapping>
</web-app>
4.> I have added mysql-connector-java-5.0.8-bin.jar and servlet-api-3.0.jar is anything else needed to add it. I have created a connection to database in Data Source Explorer.
When I m compiling or debuging it shows no error. During runtime also there is no error.
When I fill in the form and submit it shows only blank screen :(
No output is displayed. :(
Also in the database values are not updated.
Please help me. I m getting hangover with this snippet.
Check that doPost() method of servlet is called from the jsp form and remove conn.commit.
Can you check value of i by putting logger or println(). and check with closing db conn at the end. Rest your code looks fine and it should work.
Same problem fetch main problem in PreparedStatement use simple statement then you successfully insert record same use below.
String st2="insert into
user(gender,name,address,telephone,fax,email,
destination,sdate,edate,Participant,hcategory,
Culture,Nature,People,Cities,Beaches,Festivals,username,password)
values('"+gender+"','"+name+"','"+address+"','"+phone+"','"+fax+"',
'"+email+"','"+desti+"','"+sdate+"','"+edate+"','"+parti+"',
'"+hotel+"','"+chk1+"','"+chk2+"','"+chk3+"','"+chk4+"',
'"+chk5+"','"+chk6+"','"+user+"','"+password+"')";
int i=stm.executeUpdate(st2);
In your JSP at line <form> tag,
try this code
<form name="registrationform" action="Register" method="post">
String user = request.getParameter("uname");
out.println(user);
String pass = request.getParameter("pass");
out.println(pass);
Class.forName( "com.mysql.jdbc.Driver" );
Connection conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/rental","root","root" ) ;
out.println("hello");
Statement st = conn.createStatement();
String sql = "insert into login (user,pass) values('" + user + "','" + pass + "')";
st.executeUpdate(sql);
Remove conn.commit from Register.java
In your jsp change action to :<form name="registrationform" action="Register" method="post">
I had a similar issue and was able to resolve it by identifying which JDBC driver I intended to use. In my case, I was connecting to an Oracle database. I placed the following statement, prior to creating the connection variable.
DriverManager.registerDriver( new oracle.jdbc.driver.OracleDriver());
This is my JSP File. It has 3 text fields and a submit button..
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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>Insert title here</title>
</head>
<body>
<form action="buttontoserv" method="post">
<input type="text" name="name"/><br>
<input type="text" name="group"/>
<input type="text" name="pass"/>
<input type="submit" value="submit">
</form>
</body>
</html>
This is my web.xml
<?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"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<servlet>
<servlet-name>ButtontoServ</servlet-name>
<servlet-class>pack.exp.ButtontoServServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ButtontoServ</servlet-name>
<url-pattern>/buttontoserv</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
This is the servlet under pack.exp package with file name ButtontoServServlet.java
package pack.exp;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.*;
#SuppressWarnings("serial")
public class ButtontoServServlet extends HttpServlet
{
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
String name = request.getParameter("name");
String group = request.getParameter("group");
String pass = request.getParameter("pass");
System.out.println("Name :"+ name);
System.out.println("group :"+ group);
System.out.println("pass :"+ pass);
}
}
When I am deploying it to the google app engineit is throwing this error
"Error: HTTP method GET is not supported by this URL"
I also tried on tomcat and the error says
"HTTPO 405 Method not allowed. The website cannot display the page HTTP 405
Most likely cause: •The website has a programming error."
As your servlet has only doPost method. So, you can't get access the servlet with URL. Your URL should be for JSP page where action="buttontoserv" is assigned. When you click the submit button of JSP page than it will forwarded to /buttontoserv servlet.
To solve your problem, you should include a doGet method on Servlet or forward to Servlet with form submit from JSP page.
public class ButtontoServServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
String name = request.getParameter("name");
String group = request.getParameter("group");
String pass = request.getParameter("pass");
System.out.println("Name :"+ name);
System.out.println("group :"+ group);
System.out.println("pass :"+ pass);
}
#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);
}
}
Make sure you type the url to the jsp file instead of the servlet. Also try overriding the doGet method too like:
protected void doGet (HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
doPost(request, response);
}