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?
Related
Problem: When I invoke url (url-mapping) directly in the browser it works pretty well but when I use post method to invoke servlet from jsp file, it does not work but gives an error:
Type Status Report
Message /HelloWorld/myservlet
Description The origin server did not find a current representation
for the target resource or is not willing to disclose that one exists.
Jsp page:
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Hello World</title>
</head>
<body>
<form method="post" action="myservlet">
<input type="submit" value ="send">
</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://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/javaeehttp://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>helloworld2</display-name>
<servlet>
<servlet-name>myservlet</servlet-name>
<servlet-class>apress.helloworld.HelloWorld</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>myservlet</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
Servlet code:
package apress.helloworld;
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 HelloWorld extends HttpServlet{
protected void doGet(HttpServletRequest request, HttpServletResponse response)
{
//System.out.println("Get Method Called");
try { response.setContentType("text/html");
`enter code here` PrintWriter printwriter = response.getWriter();
printwriter.println("<h2>");
printwriter.println("Hello World");
printwriter.println("</h2>");
}
catch
(IOException ex)
{ ex.printStackTrace(); }
}
#Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
try { resp.setContentType("text/html");
PrintWriter printwriter = resp.getWriter();
printwriter.println("<h2>");
printwriter.println("Hello World");
printwriter.println("</h2>");
}
catch
(IOException ex)
{ ex.printStackTrace(); }
}
}
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Hello World</title>
</head>
<body>
<form method="post" action="hello">
<input type="submit" value ="send">
</form>
</body>
</html>
use Above code
in HTML form action="url-mapping" you have to mention url pattern of servlet not servlet name refer your web.xml
I am trying to pass a value to servlet through a custom jQuery confirmation box.
I searched the web and found that we can use $post to pass the data to servlet but somehow data was not getting passed to servlet.
My requirement is I need to provide two options to user to open or save the pdf document. I used custom jQuery box with these options but data is not getting passed to servlet.
Below is my code:
<%# 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>
<link rel="stylesheet" href="https://code.jquery.com/ui/1.11.2/themes/redmond/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.11.2.js"></script>
<script src="https://code.jquery.com/ui/1.11.2/jquery-ui.js"></script>
<!-- User Defined Js file -->
<script type="text/javascript">
$(document).ready(function(){
$('#Export').click(function(event) {
event.preventDefault();
var currentForm = $(this).closest('form');
var dynamicDialog = $('<div id="conformBox">'+
'<span style="float:left; margin:0 7px 20px 0;">'+
'</span>Open or save the document</div>');
dynamicDialog.dialog({
title : "Open/Save Dialog",
closeOnEscape: true,
modal : true,
buttons :
[{
text : "Export",
click : function() {
$(this).dialog("close");
$.POST({
type: "post",
url: "button",
data: $('#Export').attr("name"),
success: function(data) {
$('#results').show();
$('#results').html(data);
}
});
// currentForm.submit(); // if I use this then control going to servlet but data is not passed without nothing was happening
}
},
{
text : "Open",
click : function() {
$(this).dialog("close");
currentForm.submit();
}
}]
});
return false;
});
});
</script>
</head>
<body>
<form id="god" action="button">
<button type="button" id="Export">Export</button>
</form>
<div id="results"></div>
</body>
</html>
Servlet code:
package com.testcase.testing;
import java.io.IOException;
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 button
*/
#WebServlet("/button")
public class button extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* #see HttpServlet#HttpServlet()
*/
public button() {
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
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
response.getWriter().append(request.getParameter("data"));
System.out.println(request.getParameter("data"));
}
}
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>Testcase</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>
<servlet-name>button</servlet-name>
<servlet-class>com.testcase.testing.button</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>button</servlet-name>
<url-pattern>/Buttonpage.jsp</url-pattern>
</servlet-mapping> -->
</web-app>
Unable to trace what I am doing wrong.
This is how to send data using $.post
var param = "value";
$.post('query.php', { param: param }, function(data) {
//do what you want with returned data
})
I am currently developing a Java Web Dynamic Project and I have a Menu bar in my html page. When a user clicks one of the available options appearing on the menu, I want to be able to control which part of the code, in the matching Servlet, will be executed.
Your menu item is associated with a link, similar to this:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>simple Servlet</title>
</head>
<body>
<ul>
<li>that Action</li>
<li>this Action</li>
</ul>
</body>
</html>
The deployment descriptor:
<?xml version="1.0" encoding="UTF-8"?>
<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">
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>de.so.ActionServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>/action</url-pattern>
</servlet-mapping>
</web-app>
And the servlet code:
package de.so;
import java.io.IOException;
import java.io.Writer;
import javax.servlet.ServletException;
import javax.servlet.http.*;
public class ActionServlet extends HttpServlet
{
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException
{
String action = request.getParameter("action");
Writer out=response.getWriter();
if (action == null || action.isEmpty())
{
out.write("Action empty");
}
else if (action.equals("doThis"))
{
out.write("perform this Action");
}
else if (action.equals("doThat"))
{
out.write("perform that Action");
}
}
}
Hi I am receiving the 404 HTTP status while submiting the below jsp.
HTTP Status 404 - /TestServlet1
Could you please help me to resolve this error
Note : The person and the dog class were defined
index.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<html>
<head>
</head>
<body>
<form id="a" action="/TestServlet1">
<input type="submit">
</form>
<%
%>
Name = '${person.name}'
Dog = '${person.dog.name}'
</body>
</html>
TestServlet1
package test;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
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 TestServlet
*/
public class TestServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* #see HttpServlet#HttpServlet()
*/
public TestServlet() {
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
Dog g = new Dog();
g.setName("dogeee");
Person p = new Person();
p.setDog(g);
p.setName("xxx");
request.setAttribute("person", p);
RequestDispatcher dispatch = request.getRequestDispatcher("/index.jsp");
dispatch.forward(request, response);
}
/**
* #see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
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>GlobalWeather</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>TestServlet</servlet-name>
<servlet-class>test.TestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TestServlet</servlet-name>
<url-pattern>/TestServlet1</url-pattern>
</servlet-mapping>
</web-app>
First and Third answers are right. Either write action="TestServlet1" or action="/Projectname/TestServlet1". If you put /testServet1 in action, it means you are specifying the path the desired file which in this case is wrong while if you use testservlet1 in action , it means you are searching for a file name testservlet1 in your project to run.
404 means that the URL was not found. I suspect that you need the web application name in your URL.
So rather than using a form action of:
/TestServlet1,
try
/name_of_your_web_app/TestServlet1
Just remove tha / in your form action:
<form id="a" action="/TestServlet1">
change it to
<form id="a" action="TestServlet1">
In HTML, adding a / means the relative URL and without a slash means absolute URL. Or better to use the context as mentioned here:
<form id="a" action=${pageContext.request.contextPath}/TestServlet1>
you have to put your contextroot + your servlet name...
contextRoot usually is the name of your project.
action="/nameProject/TestServlet1"
I hope this helps you
i have problem how to call post method in java.When i pressed click button on login page after that did not show any thing on the browser.please tell me proper solution.my output show on console.so please tell me what is mistake.thanks in advance.
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">
<link rel="stylesheet" type="text/css" href="css/loginPage.css" />
<!-- <script src="js/jquery-ui.1.10.4.js"></script> -->
<title>Login Page</title>
</head>
<section class="login">
<div class="titulo">Staff Login</div>
<form action="/AdminLogin" name="AdminLogin" method="post">
<input type="text" title="Username required" placeholder="Username" data-icon="U">
<input type="password" title="Password required" placeholder="Password" data-icon="x">
<div class="olvido">
<div class="col">Register Student</div>
<div class="col">Fotgot Password?</div>
</div>
<button class="enviar">submit</button>
</form>
</section>
</html>
AdminLogin.java
package com.admin.main;
import java.beans.Statement;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
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 com.admin.dao.UserMasterConnection;
#WebServlet("/AdminLogin")
public class AdminLogin extends HttpServlet {
private static final long serialVersionUID = 1L;
Statement stmt;
UserMasterConnection userMasterConnection = new UserMasterConnection();
public AdminLogin()
{
super();
}
public void init(ServletConfig config) throws ServletException {
}
protected void service(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
System.out.println("doget");
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{
System.out.println("doPost()");
/*System.out.println("dopost");
// connection with database
// Connection connection =
Connection connection = userMasterConnection.getConnection();
String selectTableSQL = "select * from user_detail where username=? and password=?";
try {
stmt = connection.createStatement();
// execute select SQL stetement
ResultSet rs = stmt.executeQuery(selectTableSQL);
while (rs.next())
{
// get username and password from login page
String username = request.getParameter("password");
String password = request.getParameter("username");
System.out.println(username);
}
} catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}*/
String username = request.getParameter("Password");
String password = request.getParameter("Username");
System.out.println("username :-"+username);
System.out.println("password"+password);
}
}
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>SBTsystemAdminView</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>
<servlet-name>loginPage</servlet-name>
<servlet-class>com.Admin.main.AdminLogin</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>loginPage</servlet-name>
<url-pattern>/loginPage/*</url-pattern>
</servlet-mapping>
</web-app>
Get rid of the empty service method override. Its making the servlet do absolutely nothing. Read the documentation for HTTPServlet concerning the service method:
protected void service(HttpServletRequest req,
HttpServletResponse resp)
throws ServletException,
java.io.IOException
Receives standard HTTP requests from the public service method and dispatches them
to the doXXX methods defined in this class. This method is an HTTP-specific version
of the Servlet.service(javax.servlet.ServletRequest,
javax.servlet.ServletResponse) method. There's no need to override
this method.
In other words, the standard implementation of this method figures out whether the request is GET or POST and routes to the doGet or doPost functions. If you override this method, especially with a blank function, then you are preventing the doGet and doPost functions from ever being reached.