Getting Error When sending data from JSP to Servlet - java

I am using Dynamic Web Project.
This is my JSP Code. I am trying to send Hello to servlet
<%# 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>
<jsp:include page="/servlet/ServletCode" flush="true" >
<jsp:param name="username" value="Hello" />
</jsp:include>
</body>
</html>
This is my Servlet File.
package pack.exp;
public class ServletCode extends HttpServlet
{
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
String output= request.getParameter("username");
System.out.println(output);
PrintWriter pw = response.getWriter();
pw.println("Hello " + output);
}
}
In my JSP File I am getting this compile time error on this line.
Fragment "/servlet/ServletCode" was not found at expected path /JSpServletCode/WebContent/servlet/ServletCode
Please Help me with this.

I think You have to map your servlet in the web.xml, and the servlet-url you have to provide in the page. Something like below whould work.
<jsp:include page="/ServletCode" flush="true" >
<servlet>
<servlet-name>Servlet1</servlet-name>
<servlet-path>pack.exp.ServletCode</servlet-path>
</servlet>
<servlet-mapping>
<servlet-name>Servlet1</servlet-name>
<url-pattern>/ServletCode</url-pattern>
</servlet-mapping>
UPDATE
This is working for me
SERVLET
#WebServlet("/ServletCode")
public class ServletCode extends HttpServlet {
private static final long serialVersionUID = 1L;
public ServletCode() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String output= request.getParameter("username");
System.out.println(output);
PrintWriter pw = response.getWriter();
pw.println("Hello " + output);
}
}
JSP
<body>
<jsp:include page="/ServletCode" flush="true">
<jsp:param name="username" value="Hello" />
</jsp:include>
</body>

this is model servlet page:
<servlet>
<servlet-name>registerServlet</servlet-name>
<servlet-class>com.example.RegisterServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>registerServlet</servlet-name>
<url-pattern>/register</url-pattern>
</servlet-mapping>
And you must change the form like this format:
<form action="register" method="post">
this is do Post method:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String uname = request.getParameter("uname");
PrintWriter pw = response.getWriter();
pw.println("Hello " + output);
System.out.println(output);
// ...
}

Related

404 when servlet is invoked but works directly when url to servlet is used from browser

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

Request a variable defined in jsp from servlet? [duplicate]

This question already has an answer here:
Authentication filter and servlet for login
(1 answer)
Closed 6 years ago.
The username of the person who logs in is present on the index page and the code works fine. But trying to access the variable username from the servlet is proving hard. Any ideas?
index.jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title><%=request.getParameter("username")%> - Cloud</title>
<link rel="stylesheet" href="css/bootstrap.css">
</head>
<header class="page-header">
<% String username = request.getParameter("username");%> // How can i get this variable?
<h1 align="center" class="inline"><%=username%> - Files</h1>
</header>
<body>
Servlet.java
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
}
?
You have to call the servlet through the jsp page
How to call servlet through a JSP page
This is an official example from oracle of how to do that:
Jsp2Servlet.jsp
<HTML>
<HEAD> <TITLE> JSP Calling Servlet Demo </TITLE> </HEAD>
<BODY>
<!-- Forward processing to a servlet -->
<% request.setAttribute("empid", "1234"); %>
<jsp:include page="/servlet/MyServlet?user=Smith" flush="true"/>
</BODY>
</HTML>
MyServlet.java
public class MyServlet extends HttpServlet {
public void doGet (HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
PrintWriter out= response.getWriter();
out.println("<B><BR>User:" + request.getParameter("user"));
out.println(", Employee number:" + request.getAttribute("empid") + "</B>");
this.getServletContext().getRequestDispatcher("/jsp/welcome.jsp").
include(request, response);
}
}
welcome.jsp
<HTML>
<HEAD> <TITLE> The Welcome JSP </TITLE> </HEAD>
<BODY>
<H3> Welcome! </H3>
<P><B> Today is <%= new java.util.Date() %>. Have a nice day! </B></P>
</BODY>
</HTML>
The key is in the line:
<jsp:include page="/servlet/MyServlet?user=Smith" flush="true"/>
In this example a jsp call a servlet a that servlet call another jsp. The example is from this page:
https://docs.oracle.com/cd/A87860_01/doc/java.817/a83726/basics4.htm

Result redirection not working with jsp

I'm getting a ping result in my servlet.I'm trying to redirect it to another jsp file.
the jsp file for output opens.But nothing shows in it.
This is my servlet main code
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String ip = request.getParameter("ip");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
// out.println("The ip address is:"+ip+"\n");
String result = pingTest(ip);
out.println(result);
String redirect = "Output.jsp";
RequestDispatcher view = request.getRequestDispatcher(redirect);//Is it good approach to redirect request in ajax based servlet?
view.forward(request, response);
}
This is my output.jsp page
<%# 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>Ping Check Result</title>
</head>
<body>
</body>
</html
Do I need to add anything in output.jsp?
In your servlet:
request.setAttribute("result", result);
request.getRequestDispatcher("/WEB-INF/Output.jsp").forward(request, response);
In your JSP:
<pre>The data from servlet: ${result}</pre>
your servlet must be :
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String ip = request.getParameter("ip");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("The ip address is:"+ip+"\n");
String result = pingTest(ip);
out.println(result);
RequestDispatcher view = request.getRequestDispatcher();
view.forward(request, response);
}

I am new to jsp and google app engine getting this error: HTTP method GET is not supported by this URL

This is my JSP File. It has 3 text fields and a submit button..
<%# 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="buttontoserv" method="post">
<input type="text" name="name"/><br>
<input type="text" name="group"/>
<input type="text" name="pass"/>
<input type="submit" value="submit">
</form>
</body>
</html>
This is my 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/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<servlet>
<servlet-name>ButtontoServ</servlet-name>
<servlet-class>pack.exp.ButtontoServServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ButtontoServ</servlet-name>
<url-pattern>/buttontoserv</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
This is the servlet under pack.exp package with file name ButtontoServServlet.java
package pack.exp;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.*;
#SuppressWarnings("serial")
public class ButtontoServServlet extends HttpServlet
{
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
String name = request.getParameter("name");
String group = request.getParameter("group");
String pass = request.getParameter("pass");
System.out.println("Name :"+ name);
System.out.println("group :"+ group);
System.out.println("pass :"+ pass);
}
}
When I am deploying it to the google app engineit is throwing this error
"Error: HTTP method GET is not supported by this URL"
I also tried on tomcat and the error says
"HTTPO 405 Method not allowed. The website cannot display the page HTTP 405
Most likely cause: •The website has a programming error."
As your servlet has only doPost method. So, you can't get access the servlet with URL. Your URL should be for JSP page where action="buttontoserv" is assigned. When you click the submit button of JSP page than it will forwarded to /buttontoserv servlet.
To solve your problem, you should include a doGet method on Servlet or forward to Servlet with form submit from JSP page.
public class ButtontoServServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
String name = request.getParameter("name");
String group = request.getParameter("group");
String pass = request.getParameter("pass");
System.out.println("Name :"+ name);
System.out.println("group :"+ group);
System.out.println("pass :"+ pass);
}
#Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
#Override
protected void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
}
Make sure you type the url to the jsp file instead of the servlet. Also try overriding the doGet method too like:
protected void doGet (HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
doPost(request, response);
}

doPost() of GAE servlet doesn't work

I'm begginer in JSP and I have the following servlet:
#SuppressWarnings("serial")
public class HelloAppIgorServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
RequestDispatcher disp = req.getRequestDispatcher("/mainpage.jsp");
disp.forward(req, resp);
}
#Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
PrintWriter pw = resp.getWriter();
pw.print("Test");
pw.close();
}
}
and one JSP file called mainpage.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>
</head>
<body>
<p>Hello there!<p>
<form action="." method="post">
<input type="text" name="name1" />
<input type="submit" value="name2" />
</form>
</body>
</html>
The problem is that doPost() method doesn't work. It's redirecting me to index.html page when I click the button. I'm sure that problem is not in servlets, so where can it be?
You need to set the appropriate action in <form action="." method="post">. The action is the (relative) URL of the servlet that you defined via <servlet-mapping> in the web.xml.

Categories

Resources