This question already has answers here:
Servlet returns "HTTP Status 404 The requested resource (/servlet) is not available"
(19 answers)
Closed 5 years ago.
I am trying to connect html to a servlet to get the user input but I get an error that says The requested resource is not available. I even put the servlet on the same folder as the html file, but the error keeps coming. I have the servlet both in Java resources and in html folder (web content). It should be visible. Below is my code for html file and servlet.
html file:
<!DOCTYPE html>
<html>
<!--This is the login page.-->
<head>
<meta charset="ISO-8859-1">
<title>Login Page</title>
<style>
body {
background-color: lightblue;
backroung-repeat: repeat-1 ;
}
h1 {
color: maroon;
margin-left: 40px;
}
</style>
</head>
<body background="https://cdn4.iconfinder.com/data/icons/gray-user-management/512/login-512.png" >
<h1>Autentification</h1>
<form action="ConnectionMaker" method="post">
User name:<Input type="text" name="user"><br/><br/>
Password:<Input type="password" name="pass"><br/><br/>
<input type="submit" value="Submit">
login
</form>
</body>
</html>
and servlet:
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 ConnectionMaker
*/
#WebServlet("/DBConnection")
public class ConnectionMaker extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* #see HttpServlet#HttpServlet()
*/
public ConnectionMaker() {
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());
}
/**
* #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);
String user = request.getParameter("user");
String pass= request.getParameter("pass");
System.out.println(user+" "+pass);
}
}
Change <form action="ConnectionMaker" method="post"> to <form action="DBConnection" method="post"> or maybe /DBConnection (with slash). You hit the URL, not the class name.
Also, why call doGet inside doPost?
Related
I am working on a student management project in which I created two servlets in same folder, LoginController for handling the login and TestContoller for registering new student. The problem is that when I try to login LoginController is accessible and I am able to login but when i try to register a student through a register.jsp page mapped to TestController , I get HTTP Status 404 – Not Found. The requested resource [/StudentManagement/TestController] is not available error
Here is my code:-
register.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/TestController" method="post">
<input type="text" name="name">
<input type="submit" value="submit">
</form>
</body>
</html>
TestController
package com.controller;
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 TestController
*/
#WebServlet("/TestController")
public class TestController extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* #see HttpServlet#HttpServlet()
*/
public TestController() {
super();
// TODO Auto-generated constructor stub
}
/**
* #see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
/**
* #see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println(request.getParameter("name"));
}
}
I am trying to override the doGet and doPost and trying to call the doPost but not working. Below is ths JSP and Servlet code
<%# page language="java" %>
<!DOCTYPE html>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<form method="post" action="FirstServlet">
<input type="text" name="Name" value="Enter">
<input type="button" name="submit" value="Submit"/>
</form>
</body>
</html>
And Servlet code is below and trying to invoke the doPost method but it's not getting called and not printing the message in console. However when i try to access the servlet directly from URL, it's doGet method is getting called
import java.io.IOException;
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;
/**
* Servlet implementation class FirstServlet
*/
#WebServlet("/FirstServlet")
public class FirstServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* #see HttpServlet#HttpServlet()
*/
public FirstServlet() {
super();
System.out.println(" Inside Constructor");
// TODO Auto-generated constructor stub
}
/**
* #see Servlet#init(ServletConfig)
*/
public void init(ServletConfig config) throws ServletException {
// TODO Auto-generated method stub
System.out.println(" Inside init");
}
/**
* #see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println(" Inside doGet");
response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* #see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println(" Inside doPost");
doGet(request, response);
}
}
Your HTML form submit button is incorrect: fix type attribute.
Try:
<input type="submit" name="submit" value="Submit"/>
I am very very new to this. and yeah it's a homework.
I am trying to make an HTML page with a form to send some variables to a servlet and the servlet sends an applete to the same form page. so far i succeeded in sending the applet alone. I tried using jQuery, but i couldn't send the HTML tags. I can only send text.
My code so far:
index.html
<html>
<head>
<title> title </title>
</head>
<body>
<form action = "Servlet">
<input type="text" name="Name" value="" size="5" />
<input type="submit" value="Submit" name="Submit" />
</form>
</body>
Servlet.java
package ServletPackage;
import java.awt.Font;
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;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class Servlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public Servlet() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html");
String nm;
nm = request.getParameter ("Name");
PrintWriter mess = response.getWriter();
mess.println("<html>");
mess.println("<head>");
mess.println("</head>");
mess.println("<body>");
mess.println("<b> You've entered Name: </b>" + nm);
mess.println("<p align=center>");
mess.println("<applet code='NameInShape.class' width=200 height=200>");
mess.println("<param name='NAME' value='" + nm + "'>");
mess.println(" </applet>");
mess.println("</p>");
mess.println("</html>");
mess.println("</body>");
mess.close();
}
public static void main( String[] args )
{ }
/**
* #see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
All help appreciated
One way to do it is by using RequestDispatcher to call a .jsp (JavaServer Pages) clone of your index.html with the parameters posted by your form. And don't worry, it's pretty simple to implement. The result will look like the initial page the form still available, but the applet will be called using the specified parameters.
You need to import RequestDispatcher (import javax.servlet.RequestDispatcher;) and then you can do something like this in your doPost method:
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
RequestDispatcher rd = request.getRequestDispatcher("clone.jsp");
rd.forward(request, response);
}
The method rd.forward(request, response); makes the parameters posted to your servlet available on the clone.jsp page, and you can insert them using <%=request.getParameter("ParameterName")%> inside the .jsp page.
For example, after adding the above code in your servlet, you could insert something like this in the clone.jsp page to embed the applet with a parameter received from the index.html form submission:
<applet code=Applet.class width=300 height=300>
<param name=NAME value="<%=request.getParameter("NAME")%>">
</applet>
This question already has answers here:
Servlet returns "HTTP Status 404 The requested resource (/servlet) is not available"
(19 answers)
Closed 6 years ago.
I've got a page here. When I click the submit button, I get the 404 page saying:
HTTP Status 404 - /Email/EmailGet
type Status report
message /Email/EmailGet
description The requested resource is not available.
Apache Tomcat/7.0.47
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>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="EmailGet" method="GET">
Email: <input type="text" name="email"> <br> Password: <input
type="password" name="password"> <br> <input
type="submit" value="Submit">
</form>
</body>
</html>
Here's the EmailGet code:
package email;
import java.io.IOException;
import javax.mail.Session;
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;
import javax.servlet.http.HttpSession;
/**
* Servlet implementation class Controller
*/
#WebServlet("/EmailGet")
public class EmailGet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* #see HttpServlet#HttpServlet()
*/
public EmailGet() {
super();
}
/**
* #see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Gets the entered email and password
String username = request.getParameter("email");
String password = request.getParameter("password");
//Creates the session and sets the session attributes
HttpSession session = request.getSession();
session.setAttribute("username", username);
session.setAttribute("password", password);
RequestDispatcher dispatcher;
//Calls the setCredentials method to check if entered credentials are valid
boolean result = SendSMTPMail.setCredentials(username, password);
//if valid, forwards to send page
if (result == true) {
dispatcher = request.getRequestDispatcher("send.jsp");
dispatcher.forward(request, response);
}
//if not valid, forwards to index page with error message displayed
else {
dispatcher = request.getRequestDispatcher("indexerror.jsp");
dispatcher.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
}
}
Any ideas? It used to forward the page fine, but seems not to be working now.
Each jsp page runs fine individually, it's just the forwarding that isn't working.
It seems. . . that the requested page does not exist. I don't know the names of you files, so i can only guess that you have a typo somewhere, try to check your url bar and check if this is really the url you wanted.
I am trying to pass basic values such as id from jsp to the servlet through ajax. I tried everything but only null is being passed. Even console.log(val) does not print anything to browser console.
My understanding is: Web page has form values which onsubmit calls js file. js has ajax which calls the servlet and passes the data of the form. The servlet grabs data from ajax by request.getParameter(val)
Here is my code:
Main.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>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js" type="text/javascript">
<script src="js/main.js" type="text/javascript"></script>
</head>
<body>
<form method="post" action="Main" id="firstform">
<h1>Enter name:</h1>
<input type="text" name="id" id="id" />
<input type="submit" name="submit"/>
</form>
</body>
</html>
main.js
var form = $('#firstform');
console.log("gi");
form.submit(function()
{
$.ajax({
url: 'Main',
data: form.serialize(),
type: 'post',
success: function(data){
console.log(data);
}
});
//return false;
});
Main.java
package servlets;
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 Main
*/
#WebServlet("/Main")
public class Main extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* #see HttpServlet#HttpServlet()
*/
public Main() {
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
int ids;
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String val = request.getParameter("id");
System.out.print(val);
if(val != null){
ids = Integer.parseInt(val);
out.print(ids); //
}
}
/**
* #see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
**Problems:
1)values passed from jsp to servlet
2)console.log doesnt print anything on browser console
1) works but 2) still doesnt.**
in main.js type is type: 'post' and you have written code in get method
do type:'get'
there is no name attribute in your input field. when you are doing
String val = request.getParameter("id");
then in servlet then it will search for the input field having name="id" but in your form there is nothing so it will return null;
give name to the input field like
<input type="text" id="id" name="id"/>
also as sanjay has said your ajax has type post so change it to get as well
Just for the console.log(data) problem, may be $.ajax() function get confused with response type, try this:
Ajax
$.ajax({
url: 'Main',
data: form.serialize(),
type: 'post',
dataType:'text/plain',
success: function(data){
console.log(data);
}
});
Servlet
response.setContentType("text/plain;charset=UTF-8");