Below are the web.xml, servlet and jsp code
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;
/**
* Servlet implementation class MyServlet
*/
public class MyServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* #see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String userName = request.getParameter("userName");
PrintWriter out = response.getWriter();
out.println("hello "+userName+" how are you ?");
}
}
JSPCode:
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
This is First Servlet
<form action="firstTest">
<table>
<tr>
<td>Username:</td>
<td><input type="text" name="userName"/></td>
</tr>
<tr>
<td> Password:</td>
<td><input type="password" name="password" /></td>
</tr>
<tr>
<td colspan="2"><input name = "sumbit "type="submit" /></td>
</tr>
</table>
</form>
</body>
</html>
Web.xml Code:
<?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>Trial</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>FirstServlet</servlet-name>
<servlet-class>MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FirstServlet</servlet-name>
<url-pattern>/firstTest</url-pattern>
</servlet-mapping>
</web-app>
when i give URL http:localhost:8080/Trial the Index.jsp is coming but when i gave username and password the URL is chaning to http:localhost:8080/firstTest instead of http:localhost:8080/Trial/firstTest and i am getting 405 error "Tomcat error HTTP Status 405 - HTTP method GET is not supported by this URL" is anything wrong in my code
typo...
protected void doGost
change to doPost or doGet...
In your jsp code form tag you have not specified method name, and by default it use GET method, so just replace this code, I hope it will work :
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 MyServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String userName = request.getParameter("userName");
PrintWriter out = response.getWriter();
out.println("hello " + userName + " how are you ?");
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
For to resolve this type of typo errors before we deploy into the server
We use #override annotation for to test whether the method is overloaded method or not. When ever you compiled the java it will throw exception. Actually compiler will check for syntactic errors etc. But in this scenario compiler treated doGost() is also a method by using above annotation at the compilation level only developer can understand where the problem occurred.
So. My suggition is #override for overriding methods.
Related
I am getting this error on using post method in my form-- HTTP Status 405 - HTTP method POST is not supported by this URL.
My register.html file is as below:
<html>
<head>
<title>Register form</title>
</head>
<body>
<form method="post" action="Register">
Name:<input type="text" name="name" /><br/>
Email ID:<input type="text" name="email" /><br/>
Password:<input type="text" name="pass" /><br/>
<input type="submit" value="register" />
</form>
</body>
</html>
My Register.java servlet code is as below
package Glassfish;
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 java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
/**
*
* #author Intel I 5
*/
public class Register extends HttpServlet {
#Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String name = request.getParameter("name");
String email = request.getParameter("email");
String pass = request.getParameter("pass");
try{
//loading drivers for mysql
Class.forName("com.mysql.jdbc.Driver");
//creating connection with the database
Connection con=DriverManager.getConnection
("jdbc:mysql://localhost:3306/test","user","pass");
PreparedStatement ps=con.prepareStatement
("insert into register values(?,?,?)");
ps.setString(1, name);
ps.setString(2, email);
ps.setString(3, pass);
int i=ps.executeUpdate();
if(i>0)
{
out.println("You are sucessfully registered");
}
}
catch(Exception se)
{
se.printStackTrace();
}
}
}
My Web.xml file is as below
<?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>Register</servlet-name>
<servlet-class>Glassfish.Register</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Register</servlet-name>
<url-pattern>/Register</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>
I have tried all instances provided in http error 405 for post methods in stack overflow but of no use.
Change your public void service() method and instead change it to public void doGet() and remove the call doPost(req,resp) inside the method .Let me know if this helps. This will 100℅ work !!
You need to override doPost method in your class, and handle POST requests in it.
You need also override doGet method, and provide a code to handle GET requests.
You shouldn't override service method (or at least in a way you have done it in your code). You are calling doPost(request, response); in your service method, that is borrowed from HttpServlet abstract class, and a default implementation of this method you can see here:
http://grepcode.com/file/repo1.maven.org/maven2/javax.servlet/servlet-api/2.5/javax/servlet/http/HttpServlet.java#HttpServlet.doPost%28javax.servlet.http.HttpServletRequest%2Cjavax.servlet.http.HttpServletResponse%29
351 protected void doPost(HttpServletRequest req, HttpServletResponse resp)
352 throws ServletException, IOException
353 {
354 String protocol = req.getProtocol();
355 String msg = lStrings.getString("http.method_post_not_supported");
356 if (protocol.endsWith("1.1")) {
357 resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, msg);
358 } else {
359 resp.sendError(HttpServletResponse.SC_BAD_REQUEST, msg);
360 }
361 }
that is - it sends BAD_REQUEST or METHOD_NOT_ALLOWED errors to the browser.
Help me here guys...I have added servlet jar file and mysql connector jar file but it's showing the error like HTTP Status 405 - HTTP method GET is not supported by this URL.The specified HTTP method is not allowed for the requested resource.I have attempted every possible solution but i am getting the same error.
Regform.java
package com.servlet.info;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import com.mysql.jdbc.Driver;
import java.sql.PreparedStatement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Regform extends HttpServlet {
#Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String fNM = request.getParameter("firstName");
String lNM = request.getParameter("lastName");
String eID = request.getParameter("emailID");
String uNM = request.getParameter("userName");
String pass = request.getParameter("password");
try{
//loading drivers for mysql
com.mysql.jdbc.Driver mySqlDriverClassRef = new com.mysql.jdbc.Driver();
DriverManager.registerDriver(mySqlDriverClassRef);
//creating connection with the database
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/student_form?&useSSL=false", "j2ee" ,"j2ee");
PreparedStatement ps=con.prepareStatement
("insert into student values(?,?,?,?,?);");
ps.setString(1, fNM);
ps.setString(2, lNM);
ps.setString(3, eID);
ps.setString(4, uNM);
ps.setString(5, pass);
int i=ps.executeUpdate();
if(i>0)
{
out.println("You are sucessfully registered");
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
index.html
<html>
<head>
<title>Register form</title>
</head>
<body>
<form action="/register" method="POST" >
firstName:<input type="text" name="firstName" /><br/>
lastName:<input type="text" name="lastName" /><br/>
emailID :<input type="text" name="email" /><br/>
userName:<input type="text" name="userName" /><br/>
Password:<input type="text" name="pass" /><br/>
<input type="submit" value="register" />
</form>
</body>
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" >
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Regform</servlet-name>
<servlet-class>com.servlet.info.Regform</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Regform</servlet-name>
<url-pattern>/register</url-pattern>
</servlet-mapping>
Try adding below to your Java and see if it works
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws
IOException,
ServletException {
doPost(request, response);
}
I recommend that you always write the codes you defined in the doPost and doGet methods in the doProcess method.
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.
I have little test page, but I can't get request parameters an don't understand why.
Test page have "href" links like this:
Show smth
Simple controller:
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class Controller extends HttpServlet {
#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);
}
protected void processRequest (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
StringBuilder sb = new StringBuilder("Do SMTH<br>");
sb.append("Other will be later...<br>");
String s = sb.toString();
request.setAttribute("result", s);
RequestDispatcher view = request.getRequestDispatcher("test.jsp");
view.forward(request,response);
}
}
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">
<welcome-file-list>
<welcome-file>main.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>controller</servlet-name>
<servlet-class>Controller</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>controller</servlet-name>
<url-pattern>/main.jspc</url-pattern>
</servlet-mapping>
</web-app>
and test page, where i want to show some obtained parameters:
test.jsp:
<%# page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title></title>
</head>
<body>
<%
out.print("<br>" + "Parameter: ");
request.getParameter("param");
out.print("<br>"+ "URL:");
request.getRequestURL();
out.print("<br>" + "Result:");
request.getParameter("result");
%>
</body>
</html>
But test page showing empty parameters, I don't understand why.
The use of scriptlets (those <% %> things) in JSP is old one and discouraged since the birth of taglibs(JSTL) and EL(Expression Language, those ${} things).
Use below code access your parameter :
Parameter : ${params} <br>
Result : ${result} <br>
URL : ${pageContext.request.requestURI}
I try to send some data but it seems like the Controller could not be clear found or could not handle the request.
test.jsp
<%# page errorPage="exception.jsp"%>
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<%# page session="true" import="java.io.*,java.util.*"%>
<%# page import="eng.ku.sku.exceed_vote.knt.*"%>
<form name="AddForm" action="addtext" method="POST">
<input type="hidden" name="todo" value="add">
Add text:<input type="text" name="text" />
<input type="submit" value="Add">
</form>
web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<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_2_5.xsd" version="2.5">
<servlet>
<servlet-name>exceed</servlet-name>
<servlet-class>eng.ku.sku.exceed_vote.knt.Controller</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>exceed</servlet-name>
<url-pattern>/addtext</url-pattern>
Controller.java
package eng.ku.sku.exceed_vote.knt;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
#WebServlet("/addtext")
public class Controller extends HttpServlet {
private static final long serialVersionUID = 1L;
#Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response); // Same as doPost()
}
#Override
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// Retrieve the current session, or create a new session if no session exists.
HttpSession session = request.getSession(true);
System.out.println("HIIEER");
// Retrieve the shopping cart of the current session.
// For dispatching the next Page
String nextPage = "";
String todo = request.getParameter("todo");
// Dispatch to checkout.jsp
nextPage = "/checkout.jsp";
response.sendRedirect(nextPage);
return;
}
}
I read a lot of other topics but didnt find a solution. I hope u can help me :)
Okay it was a wrong config of Tomcat.
It didn't sync the project changes.