jsp_Chaitu_Informative_Blogs

Hello...guys today I'm going to show you What is JSP and how it works?

JSP means java server pages. and all JSP files must be in the ".jsp" extension and Java server pages come under the concept of Java Servlets. In the Pre-Requirements to learn servlets, you should have an understanding of "Core Java and HTML" pages.

The main two important reasons to learn servlets are:

    i. MultiThreaded

    ii. Sessions

Webserver: it is a server that is used to store Web-Applications.

examples for webservers Apache tomcat and Amazon web services e.tc...

Database: it is used to store data/information.

examples for database MySQL database, NoSQL e.t.c...

Deployment:

The process of giving apps to the server is referred to as "Deployment". Deployment is performed after Development(Writing code).

Ex: Programmer will ready the app and deploys it on a Server and then multiple users will access that app. 

Deployment can be performed in two ways:

  i. Manual Deployment: Programmers are responsible for Development as well as Deployment.

 ii. Automated Deployment: Programmers are responsible only for Development and deployment is done by MAVEN tool, ANT tool.

Also, Read

JEE Container:

The Web-Apps are involves in both FrontEnd file(.html) and Backend file(.java).

Development: Deployment on a server and then it will be going into JEE Container

i. After Deployment, Container will segregate/separate files into different sections based on their extensions, which results in a faster response to the user.

when the application is ready for usage :

ii. User can make a request, and it is forwarded for Container, and the response will be given back for user

iii.  Container helps in Servlet LifeCycle.

iv.   Container creates Config Object and Context Object.

v.  Container manages Servlet Chaining.

Conclusion:

1. Development(writing the code(HTML+Java)).

2. Deployment(Giving app to server).

3. JEE Container will Separate the files based on extensions.

4. User makes a request and JEE Container provides a response.

Types of Projects in eclipse:

1. Java Project: if the requirement is having only ".java" files(Backend Code) then the developer will go for a normal java file that contains the main method().

2. Dynamic Web Project: if the requirement is having both .html files(Frontend code) and .java files(Backend Code) then the developer will go for a java servlet file that doesn't contain the main method().

Important folders in Dynamic web Project:

       i. "src" folder: contains Backend code(java files)

      ii. "WebContent" folder: contains Frontend code(html files)

     iii. "lib" folder: contains jar files(.jar).

Programming_Chaitu_Informative_Blogs


Types of Classes in java programs:

1. Normal java class: if the class starts execution with the main() method, then it is referred to as a normal java class.

2. Servlet class: if the class starts execution with "user request" from HTML File, then it is referred to as servlet class.

Types of Servlet classes in java:

    i. GenericServlet class.

   ii. HttpServlet class.

NOTE: Sessions is supported by HttpServlet class, whereas Sessions is not supported by GenericServlet class.

NOTE: To start working with servlet programs, we need a servlet-api.jar file.

GenericServlet:

  • GenericServlet is an abstract class, which is present in javax.servlet package.
  • Being abstract class, GenericServlet consists of one abstract method by name service().
  • The Method Signature for service() method is:
Signature: public void service(ServletRequest req, ServletResponse resp)

Syntax: for GenericServlet:

public class Sample extends GenericServlet

{

@Override

public void service(ServletRequest req, ServletResponse resp)

throws ServletException, IOException 

{

//Code

}

}

Basic Steps involved in servlet programs:

1. create a servlet class under the "src" folder.

2. create HTML File under "web content" folder.

3. add jar files required into the "lib" folder.

4. link HTML File and Servlet Class.

5. Run HTML File.

Linking HTML File and Servlet Class:

  • Programmers require two important informations:

    i. <form action="servletClassName">

           //code

       </form>

   ii. @WebServlet("/servletClassname") above servlet class.

NOTE: To Print response onto Browser, Programmers will make use of the java.io.PrintWriter class.

The Syntax for PrintWriter class is:

PrintWriter printWriter = resp.getWriter();

  • println() is used to print contents onto Browser.
Example: HTML File

OTP.html:

<!DOCTYPE html>

<html>

<head>

<meta charset="ISO-8859-1">

<title>Insert title here</title>

</head>

<body>

<form action="OTPCode">

<input type="submit" value="Generate OTP">

</form>

</body>

</html>

Example java servlet program:

OTPCode.java:

package org.jsp.app;

import java.io.IOException;

import java.io.PrintWriter;

import java.util.Random;

import javax.servlet.GenericServlet;

import javax.servlet.ServletException;

import javax.servlet.ServletRequest;

import javax.servlet.ServletResponse;

import javax.servlet.annotation.WebServlet;


@WebServlet("/OTPCode")

public class OTPCode extends GenericServlet

{

@Override

public void service(ServletRequest req, ServletResponse resp)

throws ServletException, IOException

{

Random random = new Random();

int otp = random.nextInt(10000);

if(otp<0)

{

otp = otp*-1;

}

if(otp<1000)

{

 otp = otp+1000;

}

//On Console

System.out.println("OTP is : "+otp);

//On Browser

PrintWriter printWriter = resp.getWriter();

printWriter.println("OTP is : "+otp);

}

}

Fetching UserInfo from HTML File:

  • <input> tag is used in HTML File to receive values from User.
  • Every Value given by the user must be assigned to an identifier, which is declared in the HTML file.
  • To Declare identifier in HTML file, We Make use of "name" attribute, within <input> tag.
  • In Servlet Class, Programmers will make use of getParameter() to fetch user info from HTML File, with the help of "identifier" declared in HTML File.
Syntax: String value = req.getParameter("identifierGivenInHTMLFile");

Example: Code to check PhoneNumber and generate OTP, if it is valid.

First.html:

<!DOCTYPE html>

<html>

<head>

<meta charset="ISO-8859-1">

<title>Insert title here</title>

</head>

<body>

<form action="OTPCode">

MobileNumber:<input name="ph">

<input type="submit" value="Generate OTP">

</form>

</body>

</html>

OTPCode.java:

package org.jsp.app;

import java.io.IOException;

import java.io.PrintWriter;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.util.Random;

import javax.servlet.GenericServlet;

import javax.servlet.ServletException;

import javax.servlet.ServletRequest;

import javax.servlet.ServletResponse;

import javax.servlet.annotation.WebServlet;


@WebServlet("/OTPCode")

public class OTPCode extends GenericServlet

{

@Override

public void service(ServletRequest req, ServletResponse resp) 

throws ServletException, IOException

{

    //Fetching UserInfo From HTML File

    String mob = req.getParameter("ph");

    System.out.println(mob);

    //JDBC Code

    String url = "jdbc:mysql://localhost:3306?user=root&password=12345";

    String query = "select phoneNumber from test.userinfo where phonenumber=?";

    try 

    {

    Class.forName("com.mysql.jdbc.Driver");

    Connection connection = DriverManager.getConnection(url);

    PreparedStatement preparedStatement = connection.prepareStatement(query);

    preparedStatement.setString(1, mob);

    ResultSet resultSet = preparedStatement.executeQuery();

    PrintWriter printWriter = resp.getWriter();         

    if(resultSet.next())

    {

    //OTP Program

    Random random = new Random();

    int otp = random.nextInt(10000);

    if(otp<0)

    {

    otp = otp*-1;

    }

    if(otp<1000)

    {

    otp = otp+1000;

    }

    //On Console

    System.out.println("OTP is : "+otp);

    //On Browser

    printWriter.println("OTP is : "+otp);

    }

    else

    {

    printWriter.println("Invalid Phonenumber!! Retry");

    }

    connection.close();

    }

    catch (Exception e) 

    {

    e.printStackTrace();

    }

    }

}

All Requirements:

Jar files required:

1. servlet-api.jar

2. mysql-connector.jar


if you like this article then please share it with your friends and also follow my blog for more updates.