I have read question: HTTP POST with URL query parameters and understood that it possible to do it but with java and tomcat I cannot manage it.
I have html page:
<!DOCTYPE html>
<html>
<body>
<form action="HelloForm" method="POST">
First Name: <input type="text" name="first_name">
<br/>
Last Name: <input type="text" name="last_name"/>
<br/>
<input type="submit" value="Submit"/>
</form>
</body>
</html>
And I send http://localhost:8282/Hello.html?uri_param=pamparam clicking on submit button.
I tracked by proxy that both uri(GET like) and body(POST like) params have been sent:
Referer: http://localhost:8282/Hello.html?uri_param=pamparam
Connection: keep-alive
Content-Type: application/x-www-form-urlencoded
Content-Length: 36
first_name=Sergei&last_name=Rudenkov
But I get null when execute request.getParameter("uri_param"); inside doPost method.
So the question is: Is it possible to mix POST and GET params using tomcat?
Edited(additional info was requested):
My 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>HelloForm</servlet-name>
<servlet-class>HelloForm</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloForm</servlet-name>
<url-pattern>/HelloForm</url-pattern>
</servlet-mapping>
</web-app>
My servlet:
public class HelloForm extends HttpServlet {
String uri_param;
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
// Set response content type
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String title = "Using GET Method to Read Form Data";
String docType =
"<!doctype html public \"-//w3c//dtd html 4.0 " +
"transitional//en\">\n";
out.println(docType +
"<html>\n" +
"<head><title>" + title + "</title></head>\n" +
"<body bgcolor=\"#f0f0f0\">\n" +
"<h1 align=\"center\">" + title + "</h1>\n" +
"<ul>\n" +
" <li><b>First Name</b>: "
+ request.getParameter("first_name") + "\n" +
" <li><b>Last Name</b>: "
+ request.getParameter("last_name") + "\n" +
"</ul>\n" +
"<li><b>URI PARAM</b>: "
+ uri_param + "\n" +
"</ul>\n" +
"</body></html>");
}
// Method to handle POST method request.
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
uri_param = request.getParameter("uri_param");
doGet(request, response);
}
}
Result:
The way I see it, you added that GET-Parameter to the call to Hello.html, not to the actual Form-Submit to /HelloForm
Try this:
<!DOCTYPE html>
<html>
<body>
<form action="HelloForm?uri_param=janWasRight" method="POST">
First Name: <input type="text" name="first_name">
<br/>
Last Name: <input type="text" name="last_name"/>
<br/>
<input type="submit" value="Submit"/>
</form>
</body>
</html>
And maybe do
// Method to handle POST method request.
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
uri_param = request.getParameter("uri_param");
System.out.println("Parameter uri_param: " + uri_param);
doGet(request, response);
}
Edit
You can even see that in your screenshot: There's no ?uri_param in the URL you POSTed
Related
This question already has answers here:
Servlet returns "HTTP Status 404 The requested resource (/servlet) is not available"
(19 answers)
Closed 3 years ago.
My index looks like this:
<HTML>
<head>
<title> JSP Servlet Example</title>
</head>
<body>
<div align="center" style="margin-top: 50px;">
<form action="ServletManager">
Please enter your Username: <input type="text" name="username" size="20px"> <br>
Please enter your Password: <input type="text" name="password" size="20px"> <br><br>
<input type="submit" value="submit">
</form>
</div>
</body>
</HTML>
My servlet:
package helloweb;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
public class ServletManager extends HttpServlet {protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// reading the user input
String username = request.getParameter("username");
String password = request.getParameter("password");
PrintWriter out = response.getWriter();
out.println (
"<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" +" +
"http://www.w3.org/TR/html4/loose.dtd\">\n" +
"<html> \n" +
"<head> \n" +
"<meta http-equiv=\"Content-Type\" content=\"text/html; " +
"charset=ISO-8859-1\"> \n" +
"<title> JSP Servlet Example </title> \n" +
"</head> \n" +
"<body> <div align='center'> \n" +
"<style= \"font-size=\"12px\" color='black'\"" + "\">" +
"Username: " + username + " <br> " +
"Password: " + password +
"</font></body> \n" +
"</html>"
);
}
}
My XML:
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
JSPServletExample
<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>
<servlet-name>Hello</servlet-name>
<servlet-class>helloweb.ServletManager</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/ServletManager.java</url-pattern>
</servlet-mapping>
Here is my issue:
Firefox can’t find the file at /C:/Users/HP/IdeaProjects/ourdemo /web/ServletManager?username=hello&password=hello.`
I host at the tomcat 7 server.
The first page appears. I type in the username and password, and I get this.
Please help.
Please change the url pattern change this in web.xml from /ServletManager.java to /ServletManager and try
https://javabelazy.blogspot.com/2010/02/student-registration-form-in-java.html (Working code)
I am following derek banas' tutorial on youtube (https://www.youtube.com/watch?v=_HnJ501VK3M) and when I try to run the code the browser displays a status-404 and
Message: /Lesson41/
Description: The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
I was getting IllegalArguementsException saying something about 2 different servlets being mapped to the same url-pattern. So I removed the #WebServlet annotation.
Now I'm getting the 404 and I don't know what the cause is. I think eclipse doesn't see the sayhello.html
Here is the code:
Servlet: Lesson41.java
public class Lesson41 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String usersName= request.getParameter("yourname");
String theLang = request.getParameter("Language");
int firstNum = Integer.parseInt(request.getParameter("firstnum"));
int secondNum = Integer.parseInt(request.getParameter("secondnum"));
int sumONum = firstNum + secondNum;
response.setContentType("text/html");
PrintWriter output = response.getWriter();
output.println("<html><body><h3>Hello " + usersName);
output.println("</h3><br />" + firstNum + " + " + secondNum);
output.println(" = " + sumONum + "<br />Speaks " + theLang);
output.println("</body></html>");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
The 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">
<servlet>
<servlet-name>Lesson41</servlet-name>
<servlet-class>helloservlets.Lesson41</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Lesson41</servlet-name>
<url-pattern>/Lesson41</url-pattern>
</servlet-mapping>
</web-app>
html: sayhello.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
</head>
<body>
<form method="post" action="http://localhost:8080/Lesson41/">
What's your name?<br />
<input name="yourname" /><br />
First Number<br />
<input name="firstnum" /><br />
Second Number<br />
<input name="secondnum" /><br />
<input type="hidden" name="Language" value="English" /><br />
<input type="submit" />
</form>
</body>
</html>
The directory structure:
directory Structure
Expected Output:
It should show a form with fields for name, number1, number2 and a submit button. It correctly goes to localhost:8080/Lesson41/ but can't see the html.
Update the url-pattern to /Lesson41/
<url-pattern>/Lesson41/</url-pattern>
form action change to /Lesson41/ and try. might help you.
<form method="post" action="/Lesson41/">
</form>
Also, while mapping you are using servlet-class as 'helloservlets.Lesson41', need to add the package. 'helloservlets' in servlet 'Lesson41'.
I am trying to handle input from a basic HTML form in Java. However, whenever I click the submit button on the form I get this error: "The requested URL /formHandler was not found on this server."
Following are the form and the Java servlet, both of which live in the same directory, along with all the other files for this site. Any ideas why the servlet is not being found?
FORM
<form method="post" action="formHandler">
<label for="name">Name: </label>
<input name="name" defaultValue="name"/><br/>
<label for="email">Email: </label>
<input name="email" defaultValue="email"/><br/>
<label for="details">Further Details or Comments</label><br/>
<input name="details" defaultValue="details"/><br/><br/>
<input type="submit" value="Submit"/>
</form>
SERVLET
#WebServlet("/formHandler")
public class FormHandler extends HttpServlet {
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("name");
String email = request.getParameter("email");
String details = request.getParameter("details");
PrintWriter writer = response.getWriter();
String htmlRespone = "<html>";
htmlRespone += "<h2>Thank you " + name + ", a confirmation email will be sent to " + email + " shortly.</h2>";
htmlRespone += "</html>";
writer.println(htmlRespone);
}
}
This is my Java code and HTML code.
When the user clicks on the Redirect button, I want to redirect him to the log-in page and kill the current session. I have succeeded in the redirection part but I do not know how to kill the session. I would like to use the session.invalidate in the action= in HTML. Can this be done?If not then what the best alternatives? Thx.
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.WebServlet;
import java.io.*;
#WebServlet("/test1")
public class test1 extends HttpServlet{
public void init(){
System.out.println("init is called");
}
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException,IOException{
res.setContentType("text/html");
PrintWriter pw=res.getWriter();// used to type in the browser
String s=req.getParameter("username");
String s2=req.getParameter("pwd");
// Create a session object if it is already not created.
HttpSession session = req.getSession(true);
if(s.equals("ace")&& s2.equals("123"))
{
//pw.println("<h1>"+"Welcome to the world of servlets "+s+"</h1>");
String title= "Welcome to the world of servlets: "+s;
String s3="The session id is: "+session.getId();
String docType =
"<!doctype html public \"-//w3c//dtd html 4.0 " +
"transitional//en\">\n";
pw.println(docType +
"<html>\n" +
"<head><title>" + title + "</title></head>\n" +
"<body bgcolor=\"#f0f0f0\">\n" +
"<h1 align=\"center\">" + title + "</h1>\n" +
"<h1 align=\"center\">" + s3 + "</h1>\n"+
"<ul>\n" +
"<input type=\"button\" onclick=\"location.href('http://localhost:8080/AkshayServlet2/test.html');\" value=\"Redirect\">"+
"</ul>\n" +
"</body></html>");
session.invalidate();
//System.out.println.print("You have logged out");
}
else
{
pw.println("Please enter the correct username and password");
req.getRequestDispatcher("test.html").include(req, res);;
}
}
public void destroy(){
System.out.println("calling destroy method");
}
}
HTML code:
<html>
<body>
<form action="test1" method=get> <!-- we use xy because we used xy in the url in web.xml. We have to map it to the url -->
Enter UserName: <input type=text name=username><br>
Enter Password: <input type=password name=pwd><br>
<input type=submit value="Login">
</form>
</body>
</html>
You have to call the HttpServletRequest::getSession method so that a new session is created to replace the previous invalidated session.
Add the following code after the session invalidation line:
req.getSession(true);
This question already has answers here:
Servlet returns "HTTP Status 404 The requested resource (/servlet) is not available"
(19 answers)
Closed 6 years ago.
ED: this question is not a duplicate. the alleged duplicate makes no mention of the error that was in my console, and the solution to my problem is not found in the linked question.
i'm using derek banas's tutorials to learn servlets right now. or at least, i'm trying to! i keep encountering a 404 error when i submit my form.
here is my code. first, the java class:
package helloservlets;
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 Lesson41
*/
#WebServlet
public class Lesson41 extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* #see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String usersName = request.getParameter("yourname");
String theLang = request.getParameter("Language");
int firstNum = Integer.parseInt(request.getParameter("firstnum"));
int secondNum = Integer.parseInt(request.getParameter("secondnum"));
int sumONum = firstNum + secondNum;
response.setContentType("text/html");
PrintWriter output = response.getWriter();
output.println("<html><body><h3>Hello " + usersName);
output.println("</h3><br />" + firstNum + " + " + secondNum);
output.println(" = " + sumONum + "<br />Speaks " + theLang + "</body></html>");
}
/**
* #see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
my web.xml file:
<?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">
<servlet>
<servlet-name>Lesson41</servlet-name>
<servlet-class>helloservlets.Lesson41</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Lesson41</servlet-name>
<url-pattern>/Lesson41</url-pattern>
</servlet-mapping>
</web-app>
and my html:
<!DOCTYPE html>
<html>
<head>
<meta charset="US-ASCII">
<title>Insert title here</title>
</head>
<body>
<form method="post" action="http://localhost:8080/Lesson41/">
What's your name?<br /> <input name="yourname" /><br /> First
Number <input name="firstnum" /><br /> Second Number <input
name="secondnum" /><br /> <input type="hidden" name="Language"
value="English" /><br /> <input type="submit" />
</form>
</body>
</html>
in my console, i am getting this warning:
WARNING: [SetContextPropertiesRule]{Context} Setting property 'source' to 'org.eclipse.jst.jee.server:Lesson41' did not find a matching property.
and that's the only warning i'm getting. i googled it and followed the advice i found here, but it hasn't fixed my problem.
thank you in advance for any help!
Are you sure the url in the form is correct? http://localhost:8080/Lesson41/
If you are using Eclipse, by default it deploys your application as http://localhost:8080/[Your-Project-Name-Here]/Lesson41/