Open In App

Web Development Using Java Technology For Beginners

Improve
Improve
Like Article
Like
Save
Share
Report

In order to create a java web-based project with which the knowledge is up to programming language then you can follow the simple flow explained below while creating a project being a beginner. The first step geek in order to create such web development projects requires the knowledge about the web technologies carried forward to other frameworks. There are many ways to create such Java web projects, where there are many frameworks spring, maven, hibernate, etc depending on the requirement of the project. For a naive user knowledge will be missing out for Collection, framework, and web technologies but still, if someone wants to develop a proper java web application, the least idea of how to proceed further will require at least a combination of technologies to use with clear concepts of programming in any language preferably be it java or python as these days most of the tools are developed using these languages. So in order to combat these languages are preferred these days. 

Here is an approach or technology as a combination of technologies that one can follow for a simple web application who is having a basic knowledge of core java and a bit of knowledge of advanced java working with the database. In order to create a project one must be clear about the two ends, this includes frontend and backend. 

  1. Frontend refers to the part built from where the user can access the application. In a web application, web pages act as a user. And in the said approach you will use JSP and HTML pages.
  2. Backend refers to the pages that are being accessed by the user that will be handled and controlled through the backend process that needs to be developed. It is also known as “server-side” where the data records, data management, and logic controls are done.

Real-life Situation

Consider an architecture of a mall in which people working on building it by piling on bricks as cells as per the instructions received to them. These set of people are called frontend builders while the sets of people working over laying layout on a piece of paper later on dealing with science in order to give maximum stability to the foundation which is to be laid further involving research and development over it are called backend people. These people have sent instruction to frontend people after doing research, development and testing.  

Now coming back to the project, for the frontend or UI(User Interface) one can either opt for swing or JSP. Here taking JSP for the illustration part out of swing and JSP which stands for Java Server Pages

 JSP stands for Java Server Pages. If you know HTML, CSS you are less far to Know JSP(a java server page), where you use HTML tags only with extra features for which JSP acts as a dynamic web page. You can use HTML pages as well instead of JSP if you want to start with a simpler approach.

The input from users can be collected through the forms of a webpage by using JSP and the records from the database system are able to appear in the form of view respectively. In order to retrieve the information containing data from the database system, the available tags of JSP are used and these tags are used for different related functionalities like going from one page to another and similarly for other means as well

Implementation: Suppose you have a webpage like a form field, the data entered in the field(by the user) will be accessed by java programming with the help of Servlet.

Example: JSP pages are helpful to retrieve the information from the database and show it on the web pages to users.

login.jsp
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
                   <title>login here</title>
</head>
<body> 
            <form action="userlogin" method="post">
                               User Name <input type="text" name="username"><br>
                              Password <input type="password" name="password"><br>
                           <input type="submit" value="login">
           </form>
</body>
</html>

In the above example below tag is the action that defines the servlet it will direct to after the button is submitted. Servlet example on login will be seen in the latter half.

Tag: 

<form action="userlogin" method="post"> tag -> action 

Back-End: It does involve a programming language where here java is taken for the illustration part.

As you are making a project on java you should have knowledge of java definitely to work on the logic like accessing the data from the database and working on it then sent back to the server in the UI part etc, this is the main backend section where the logical part will be implemented.

Java will act as a medium that will connect  UI(need to know about Servlet) and database(need to know about JDBC) with its logic. One has to install IDE for java such as Eclipse or Netbeans, etc. 

Note: Eclipse is more preferable if using jsp instead of swing if used in frontend

Servlets are the Java programs that run on the Java-enabled web server or application server. They are used to handle the request obtained from the webserver, process the request, produce the response, then send the response back to the webserver. Though it is a java class, required in a web project. Suppose you have a webpage like form fields, the data entered in the field will be accessed by java programming with the help of Servlet. Servlet helps to redirect from web pages to servlet and these servlet classes will help to connect with other java classes.

There are some common functions you’ll require using in servlet:

Implementation: HttpServlet class is used here

Example: It is provided on the basis of JSP page example as shown above

// Importing required basic classes
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

// Importing classes from java.servlet package
// for connectivity of application class
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

// Since userlogin is used in action="userlogin"
// form tag in jsp page
@WebServlet("/userlogin")

// Class 1
// Helper class extending HttpServlet main class
public class LoginServlet extends HttpServlet 
{

    // Method
    @Override
    protected void doPost(HttpServletRequest request,
                          HttpServletResponse response)
    throws ServletException, IOException 
    {

        response.setContentType("text/html");

        PrintWriter out = response.getWriter();

        // Customly setting name and password
        String name = request.getParameter("name");
        String password = request.getParameter("password");
        ....
        // Further programming
        // Code can be appended here onward so
    }
}

Database: Most web apps require a place to store data for which database is used,e.g., suppose there is a webpage registering as a new user. Now think, where all these data will be saved? Here we can use a database that will store the data of the user, which will be accessible all the time. However, these data will be accessed with the help of backend code written in java and will be connecting with UI as well as explained in the java section as well.

As a beginner you should use a relational database, you can install:

  • MySQL
  • Oracle

JDBC: If you are using java and a database like MySQL.How will you communicate with it as you cannot use SQL commands directly? For that, you will have to use JDBC(java database connectivity) that will connect the database and there are some abstract classes like Connection, etc will have to use.

With that, you will have to add a jar file

For MySQL DB->mysql connector jar file and for 
For Oracle->ojdbc jar file

Implementation: Both connection class and application(Main) class is represented in a single frame as follows where the first frame is the connection class for the JDBC which returns out the connection class object. The second frame represents the application class of the corresponding connection class.

Example 

Java




// Java Program to Illustrate JDBC Connection In Oracle DB
 
// Importing database
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import oracle.jdbc.driver.OracleDriver;
 
// Class 1
// JDBC Connection Class
// To communicate with the database
// Note: This class is to be used in Application class
// where moto of this class is connection object
public class JDBCconnection {
 
    // Declaring connection class object and
    // initializing it to null
    private static Connection connection = null;
 
    // Method
    // Static method that connects with database
    public static Connection getConnection()
    {
 
        // Try block to check for exceptions
        try {
 
            // Loading and registering drivers
            // using DriverManager
 
            // Setting instance as Oracle driver
            // This implies database is Oracle
            DriverManager.registerDriver(
                new OracleDriver());
 
            // Passing DB- URL,username,password
            connection = DriverManager.getConnection(
                "jdbc:oracle:thin:@localhost:1521:xe",
                "username", "password");
        }
 
        // Catch block ot handle DB exception
        catch (SQLException e) {
 
            // Display the line number where exception
            // occurred using printStackTrace() method
            e.printStackTrace();
        }
 
        // If no exception caught database is connected
        return connection;
 
        // Display message successful connection
        System.out.print("Connection established");
    }
}


Java




// Java Program to Illustrate JDBC Connection In SQL DB
 
// Importing database
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
 
// Main class
// Application class
public class JdbcMySql {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Try block to check for exceptions
        try {
 
            // Setting database type for connection as MYSQL
            Class.forName("com.mysql.jdbc.Driver");
 
            // Loading and registering driver
 
            // MYSQL database iul with DB
            // name, username and password
            Connection connection
                = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/databaseName",
                    "username", "password");
 
            // If connection done with database this
            // statements will be printed otherwise exception
            // will be caught
            System.out.println("Connection established");
 
            // Closing the connections
            // using close() method
            connection.close();
        }
    }
 
    // Catch block to handle DB exceptions
    catch (SQLException e)
    {
        // Display the line number where exception occurred
        e.printStackTrace();
    }
}


Output: Generated on console

Connection established 

There are some common functions you’ll be using after database connection:

Serve: For your application, you need a server to host. The server runs the Java Servlets The easy and basic server you can use in java is Tomcat. 

  • Tomcat is a web server designed to serve files from the local system.

If you are using eclipse, you’ll have to install a tomcat server of any version and need to add a path to the application. Then, you can easily run the webpages with the respective port number provided by tomcat that will act as a local host.



Last Updated : 24 May, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads