Monday, March 8, 2021

Java Servlet simple example

 //Servlet file name 'RandomGreetings.java'.

//Run on NetBeans IDE 8.2

/*

 * A Servlet that returns a randomly chosen greeting from a list of five

 * different greetings. The greetings must be stored as constant strings

 * in the program.

 */


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 java.util.Random;

//

@WebServlet(urlPatterns = {"/RandomGreetings"})

//

public class RandomGreetings extends HttpServlet 

{   

    //Five greetings saved as constan strings

    public static final String STR1 = "Good morning";

    public static final String STR2 = "Good night";

    public static final String STR3 = "Good afternoon";

    public static final String STR4 = "Good day";

    public static final String STR5 = "Welcome";

    //

    String[] arr = {STR1,STR2,STR3,STR4,STR5};

    //

        Random ran = new Random();

    //

    protected void doGet(HttpServletRequest request, HttpServletResponse response)

            throws ServletException, IOException 

    {

        

        //

        int random = ran.nextInt(5);

        //

        String g = arr[random];

        response.setContentType("text/html");

        PrintWriter out = response.getWriter(); 

        out.println("<!DOCTYPE html>");

        out.println("<html>");

        out.println("<head>");

        out.println("<title>Random Greetings</title>");            

        out.println("</head>");

        out.println("<body>");

        out.println(g);

        out.println("</body>");

        out.println("</html>");

    }

}

-----------------------------------------------------------------------------------------------

//Corresponding HTML index file (index.html)

<!DOCTYPE html>
<!--
A simple markup document to test the Servlet
-->
<html>
    <head>
        <title>Random Greetings</title>
        <meta charset="UTF-8">
    </head>
    <body>
        <div>
            <form action="RandomGreetings" method="get">
                Press the button to test the Servlet
                <input type="submit" value="Test Servlet"/>
            </form>
        </div>
    </body>
</html>
 --------------------------------------------------------------------------------------------------

//Project directory structure at location: C:\Users\ashu\Documents\NetBeansProjects\ServletE1


-------------------------------------------------------------------------------------------------------------------------

Output:

//On running the servlet, the IDE opens the 'index.html'  page at url = http://localhost:8080/ServletE1/) 
//Syntax of index page url = http://localhost:<port_numer>/<project_name>/


//Button click executes the servlet at url = http://localhost:8080/ServletE1/RandomGreetings?
//NOTE: You need to run each servlet file one by one in the project (if it has multiple servlet files/java files in the 'Source Packages' folder)

//Syntax for the servlet url = http://localhost:<port_number>/<project_number>/<servlet_file_name_without_.java_extension>?




----------------------------------------------------------------------------------------------------------------------


No comments:

Post a Comment

Sacred Thought

5 May 2024 Hari Om Verse 50-51, chapter two:  In this chapter two Shree krishna explains a simple way of living. Free from desires and void ...