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"/>
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"));
}
}
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?
I am using Java EE. I have some problems with session. I have created Listener class and it looks like this:
package RandomPackage;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
/**
* Application Lifecycle Listener implementation class Listener
*
*/
#WebListener
public class Listener implements HttpSessionListener {
/**
* Default constructor.
*/
public Listener() {
// TODO Auto-generated constructor stub
}
/**
* #see HttpSessionListener#sessionCreated(HttpSessionEvent)
*/
public void sessionCreated(HttpSessionEvent arg0) {
System.out.print("Session Created");
arg0.getSession().setAttribute("Name", "Geoge");
}
/**
* #see HttpSessionListener#sessionDestroyed(HttpSessionEvent)
*/
public void sessionDestroyed(HttpSessionEvent arg0) {
// TODO Auto-generated method stub
}
}
Now in my HTML file I have the following code:
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1> Hello </h1>
<form action="SessionTest" method="POST">
<input type="submit">
</form>
</body>
</html>
And my SessionTest servlet looks like this:
package RandomPackage;
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;
#WebServlet("/SessionTest")
public class SessionTest extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* #see HttpServlet#HttpServlet()
*/
public SessionTest() {
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 {
System.out.println("String is " + (String)request.getSession().getAttribute("Name"));
}
}
It should have the output: "String is George" but instead it never prints "Session Created" and the output is "String is null"
Any suggestions?
i have tried the same code it is working for me see the output
New to servlets and what not. This is how my project is laid out :
Projectname
src
servlet.java
web content
META-INF
WEB-INF
first.jsp
contents of my servlet.java : pretty much it just takes a text field from jsp
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 servlet
*/
#WebServlet("/servlet")
public class servlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* #see HttpServlet#HttpServlet()
*/
public servlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* #see Servlet#destroy()
*/
public void destroy() {
// 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;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
String user=request.getParameter("string");
out.println("<h2> Welcome "+user+"</h2>");
} finally {
out.close();
}
}
}
The 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>Input Form</title>
</head>
<body>
<form action="/servlet" method="POST">
What's your name:<br>
<input type="text" name="string">
<br>
<input type="submit" value="Submit">
</form>
</body>
</html>
and finally the web.xml
<servlet>
<servlet-name>servlet</servlet-name>
<jsp-file>/first.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>servlet</servlet-name>
<url-pattern>/servlet</url-pattern>
</servlet-mapping>
The problem is that I can see the basic jsp, which is the form to submit a string on but nothing happens when I click submit. I don't even know where to look for errors and logs to figure out where the problem is.
We had a few issues, that we've discussed at the chat and at the comments:
HttpServlet.service() was being overriden with an empty implementation, so doGet() and doPost() weren't being called.
We had a jsp on web.xml overriding the Servlet annotated mapping.
The reviewed code ended up looking something like this:
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
/**
* Servlet implementation class servlet
*/
#WebServlet("/servlet")
public class servlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* #see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
*/
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Overriding service() usually isn't needed. - The default implementation mostly
// does the right thing®
super.service(request, response)
}
/**
* #see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Just forward the request to the jsp page on get requests
request.getRequestDispatcher("/first.jsp").forward(request,response);
}
/**
* #see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
String user=request.getParameter("string");
out.println("<h2> Welcome "+user+"</h2>");
} finally {
// Don't close the Response - it will mess with filters
// out.close();
out.flush()
}
}
}
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>