servlet – Programmerbay https://programmerbay.com A Tech Bay for Tech Savvy Mon, 04 Jul 2022 06:31:49 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.5 https://programmerbay.com/wp-content/uploads/2019/09/cropped-without-transparent-32x32.jpg servlet – Programmerbay https://programmerbay.com 32 32 Create an application to show how to call a servlet from another servlet using sendRedirect method in apache tomcat server https://programmerbay.com/create-an-application-to-show-how-to-call-a-servlet-from-another-servlet-using-sendredirect-method-in-apache-tomcat-server/ https://programmerbay.com/create-an-application-to-show-how-to-call-a-servlet-from-another-servlet-using-sendredirect-method-in-apache-tomcat-server/#respond Sat, 07 Dec 2019 15:20:38 +0000 https://programmerbay.com/?p=5664 There is another way to call a servlet from another servlet using sendRedirect method.
1) Create Index.html file by clicking on ‘new’ tab and select HTML.

Index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Practical Work</title>
</head>
<body>
<form action="first_servlet">
WRITE <input type="text" name="word" value="I am calling first servlet"/>
<button type="submit" name="calculate">Call First Servlet </button><br/>
</form>
</body>
</html>

2) Create a servlet that will redirect to another servlet. In order to send data, one requires to maintain a session.

First_Servlet.java

package com.programmerbay;

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 First_Servlet extends HttpServlet{
  public void service(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException
  {
  String word = request.getParameter("word")	;
  PrintWriter output = response.getWriter();
  
String word1 = word+".\n Now, It is calling second servlet";
  // redirecting it to second servlet
HttpSession session = request.getSession();
session.setAttribute("word", word1);
  response.sendRedirect("second_servlet");
  }

}

3) Write another servlet that will handle the data sent from the first servlet and print the message.

Second_Servlet.java

package com.programmerbay;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class Second_Servlet extends HttpServlet{
  public void service(HttpServletRequest request,HttpServletResponse response) throws IOException
  {
    HttpSession session = request.getSession();
    String word = (String) session.getAttribute("word");
    PrintWriter output = response.getWriter();
    output.println(word+" \n usiing session");
    
  }
}

4) Modify the Web.xml file to map servlets.

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">
  


 <servlet>
 <servlet-name>Invoke_First</servlet-name>
 <servlet-class>com.programmerbay.First_Servlet</servlet-class>
 
 </servlet>
 
 <servlet-mapping>
 
 <servlet-name>Invoke_First</servlet-name>
 <url-pattern>/first_servlet</url-pattern>
 
 </servlet-mapping>
 
 <servlet>
 <servlet-name>Invoke_Second</servlet-name>
 <servlet-class>com.programmerbay.Second_Servlet</servlet-class>
 
 </servlet>
 
 <servlet-mapping>
 
 <servlet-name>Invoke_Second</servlet-name>
 <url-pattern>/second_servlet</url-pattern>
 
 </servlet-mapping>
 
 
</web-app>

Output:

call one servlet from another

]]>
https://programmerbay.com/create-an-application-to-show-how-to-call-a-servlet-from-another-servlet-using-sendredirect-method-in-apache-tomcat-server/feed/ 0
Create an application to call a servlet from another servlet using RequestDispatcher in apache tomcat server https://programmerbay.com/create-an-application-to-call-a-servlet-from-another-servlet-in-apache-tomcat-server/ https://programmerbay.com/create-an-application-to-call-a-servlet-from-another-servlet-in-apache-tomcat-server/#respond Sat, 07 Dec 2019 15:10:49 +0000 https://programmerbay.com/?p=5660 Here’s the code to invoke a servlet from another servlet using RequestDispatcher interface.
1) Create index.html.

Index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Practical Work</title>
</head>
<body>
<form action="first_servlet">
WRITE <input type="text" name="word" value="I am calling first servlet"/>
<button type="submit" name="calculate">Call First Servlet </button><br/>
</form>
</body>
</html>

2) Now, create a basic servlet and pass the data of textbox field from index.html to First_Servlet.java. Use requestdispatcher to rely the data to other servlet.

First_Servlet.java

package com.programmerbay;

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;

public class First_Servlet extends HttpServlet{
public void service(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException
{
String word = request.getParameter("word") ;
PrintWriter output = response.getWriter();

String word1 = word+".\n Now, It is calling second servlet";
// Dispatching it to second servlet
RequestDispatcher redirecting = request.getRequestDispatcher("second_servlet");
request.setAttribute("passingWord", word1);
redirecting.forward(request, response);
}

}

3) After that, write the code for second servlet that will handle the message or information from first servlet.

Second_Servlet.java

package com.programmerbay;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Second_Servlet extends HttpServlet{
public void service(HttpServletRequest request,HttpServletResponse response) throws IOException
{
String word = (String) request.getAttribute("passingWord");
PrintWriter output = response.getWriter();
output.println(word);

}
}

4) Edit the deployment descriptor.

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">

<servlet>
<servlet-name>Invoke_First</servlet-name>
<servlet-class>com.programmerbay.First_Servlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>Invoke_First</servlet-name>
<url-pattern>/first_servlet</url-pattern>

</servlet-mapping>

<servlet>
<servlet-name>Invoke_Second</servlet-name>
<servlet-class>com.programmerbay.Second_Servlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>Invoke_Second</servlet-name>
<url-pattern>/second_servlet</url-pattern>

</servlet-mapping>


</web-app>

Output:

call one servlet from another

]]>
https://programmerbay.com/create-an-application-to-call-a-servlet-from-another-servlet-in-apache-tomcat-server/feed/ 0