What can a JSP form action be?
I have a Login.jsp page for the user to end the details.
Can i give the servlet class in the form action?
here is the the servlet code.
package mybean;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public LoginServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try
{
System.out.println("In the Login Servlet");
LoginBean user = new LoginBean();
user.setUemail(request.getParameter("uemail"));
user.setUpass(request.getParameter("upass"));
user = LoginDAO.login(user);
if(user.isValid())
{
HttpSession session = request.getSession(true);
session.setAttribute("currentSessionUser",user);
response.sendRedirect("LoginSuccess.jsp");
}else
response.sendRedirect("LoginFailed.jsp");
} catch (Throwable exc)
{
System.out.println(exc);
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
As per the specs , it can take any valid URI
This attribute specifies a form processing agent. User agent behavior for a value other than an HTTP URI is undefined.
Can i give the servlet class in the form action ?
Yes if the servlet class name resolves to a valid URL mapped in the web.xml or else you will encounter a 404 .
Let us consider your JSP is at the root of the application, then
<FORM action="someServletName" method="post">
Now this will be resolved as protocol://servername:port/context/someServletName .Now somewhere you should have a mapping for /someServletName , either in web.xml or through annotation to a Servlet or JSP.
<servlet>
<servlet-name>someServletName</servlet-name>
<servlet-path>packageName.servletName</servlet-path>
</servlet>
<servlet-mapping>
<servlet-name>someServletName</servlet-name>
<url-pattern>/someServletName</url-pattern>
</servlet-mapping>
Related
I have a problem with only one servlet, all the others (declared in the same way) work nice
Servlet with problem :
package com.myproject.servlets;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
#SuppressWarnings("serial")
public class Connexion extends HttpServlet {
public static final String VUE = "/WEB-INF/connexion.jsp";
public void doGet( HttpServletRequest request, HttpServletResponse response )
throws ServletException, IOException{
this.getServletContext().getRequestDispatcher( VUE ).forward( request, response );
}
public void doPost( HttpServletRequest request, HttpServletResponse response )
throws ServletException, IOException{
}
}
Another Servlet who works :
package com.myproject.servlets;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
#SuppressWarnings("serial")
public class Authentification extends HttpServlet {
public static final String VUE = "/WEB-INF/authentification.jsp";
public void doGet( HttpServletRequest request, HttpServletResponse response )
throws ServletException, IOException{
this.getServletContext().getRequestDispatcher( VUE ).forward( request, response );
}
public void doPost( HttpServletRequest request, HttpServletResponse response )
throws ServletException, IOException{
}
}
The JSP who call the servlet "Connexion" :
<form id="form_authentification" class="formulaire" method="post" action="connexion">
<input type="text" class="input_text" title="<fmt:message key="mail_ou_nom" />" placeholder="<fmt:message key="mail_ou_nom" />" required />
<input type="submit" class="btn" value="<fmt:message key="suivant" /> "/>
And the web.xml :
<!-- Authentification -->
<servlet>
<servlet-name>Authentification</servlet-name>
<servlet-class>com.myproject.servlets.Authentification</servlet-class>
<init-param>
<param-name>auteur</param-name>
<param-value>Atyss</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Authentification</servlet-name>
<url-pattern>/authentification</url-pattern>
</servlet-mapping>
<!-- Connexion -->
<servlet>
<servlet-name>Connexion</servlet-name>
<servlet-class>com.myproject.servlets.Connexion</servlet-class>
<init-param>
<param-name>auteur</param-name>
<param-value>Atyss</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Connexion</servlet-name>
<url-pattern>/connexion</url-pattern>
</servlet-mapping>
So If I put the method "get" in the form : "form_authentification" of the JSP authentification.jsp it works but I have in line the browser :
http://localhost:8080/myproject/connexion?
Where from "?" is coming in the line browser?
If I put the method "post" in the form : "form_authentification" of the JSP authentification.jsp I have a white page for connexion.jsp and the browser tag icon of apache server.
Thank you for your answer
i'm trying to solve this problem since 2 days, there are the same title of problems in many post on stackoverflow but it was all different, because they allways had an error in code, but in my case i have a sample servlet, and when i run the project i get this error
HTTP Status 405 - HTTP method GET is not supported by this URL
this is my class servlet
package web;
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 Controller extends HttpServlet{
/**
*
*/
private static final long serialVersionUID = 1L;
#Override
public void init() throws ServletException {
}
#Override
protected void doGet(HttpServletRequest requet, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
PrintWriter out =response.getWriter();
out.println("hello");
}
}
and my 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_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>test2</display-name>
<servlet>
<servlet-name>cs</servlet-name>
<servlet-class>web.Controller</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>cs</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
</web-app>
please tell me how solve this problem, thanks ...
Simple doGet Servlet Or You Can add try catch blog to debug code
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class logout extends HttpServlet
{
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
try
{
PrintWriter out=response.getWriter();
response.setContentType("text/html;charset=UTF-8");
out.println("Hello");
}
catch(Exception e)
{
out.println(e.toString())
}
}
}
I think you probably have not add GAT method on form attribute Like this
<form action="web" method="GET"></form>
I've a HTML form:
<form action="rest/ws/addNote" method="post">
I'm trying to POST to this servlet:
#WebServlet("/ws")
public class AddNote extends HttpServlet {
#POST
#Path("/addNote")
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// ...
}
}
But I keep getting
HTTP Status 404 - Could not find resource for relative : /ws/addNote of full path: http://localhost:8080/project/rest/ws/addNote
You are sending a post request for that you should have a post request handler method there in your servlet. I am assuming you are not using any REST framework. Then your servlet should be:
#WebServlet("/rest/ws/addNote")
public class AddNote extends HttpServlet {
private static final long serialVersionUID = 1L;
#Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
........
Or if you are already using any REST frameworks like Jersy, don't use a servlet here.Try some examples
Update
since you are using REST try following instead of servlet:
import javax.ws.rs.FormParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;
#Path("/ws")
public class AddNote {
#POST
#Path("/addNote")
public Response addUser(
#FormParam("name") String name,
#FormParam("age") int age) {
.........
Here I assumed in your web.xml, the REST controller servlet mapping is /rest/* and your html <form> having <input> tags with names name,age, then the will be passed into the corresponding method arguments as above.
Full example you can see here
I have written a simple HelloServlet as follows.
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 HelloServlet
*/
#WebServlet("/HelloServlet")
public class HelloServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* #see HttpServlet#HttpServlet()
*/
public HelloServlet() {
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.setContentType("text/html");
PrintWriter out = response.getWriter();
String docType =
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
"Transitional//EN\">\n";
out.println(docType +
"<HTML>\n" +
"<HEAD><TITLE>Hello</TITLE></HEAD>\n" +
"<BODY BGCOLOR=\"#FDF5E6\">\n" +
"<H1>Hello</H1>\n" +
"</BODY></HTML>");
}
/**
* #see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
I have GlassFish 3.1 installed in the Java EE perspective in Eclipse.
Whenever I try and run the servlet clicking on "run on server" on the server it comes up with the following message:
This domain requires an administrative password to be set before the
domain can be started. Please specify an administrative password. The
Master Password is required to start the domain. No console, no
prompting possible. You should either create the domain with
--savemasterpassword=true or provide a password file with the --passwordfile option.
Command start-domain failed.
There is always a default master password "changeit" for glassfish. Why is it prompting me with this message ?
**loginServlet.java**
package com.anil.apps;
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 LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out=response.getWriter();
String uid=request.getParameter("userid");
String password=request.getParameter("pwd");
if(uid.equals("Anil")&&password.equals("missinlx")){
//out.println("welcome "+uid);
response.sendRedirect("welcomeUser?userid="+uid);
}
else{
out.println("invalid username or password");
}
}
}
**WelcomeUserServlet.java**
package com.anil.apps;
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 WelcomeUserServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out=response.getWriter();
out.println("<html><body><h1>Welcome " +uid+ "</h1></body></html>");
}
}
what is wrong with my code?? I want to redirect the page to welcomeUser.java page using response.sendRedirect() method.
As I am new to servlet please help me out of it. please tell me the whole format for the page redirection in Servlets.
GET parameters are not turned in to variables automatically. You need to add this to the top of your doGet method in the WelcomeUserServlet:
String uid = request.getParameter("userId");
Making that class look like this:
public class WelcomeUserServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String uid = request.getParameter("userId");
PrintWriter out=response.getWriter();
out.println("<html><body><h1>Welcome " +uid+ "</h1></body></html>");
}
}
You are using the wrong argument for the sendRedirect method
You should use a complete path like
response.sendRedirect("some/path/here/to/"+welcomeUser?userid="+uid)
Or better
response.sendRedirect(response.encodeURL(response.getContextPath()+"welcomeUser?userid="+uid))
Or use a request dispatcher which knows the structure of your project and does not need a full path
request.getRequestDispatcher("welcomeUser?userid="+uid).forward(request,response)