How to Create a New Project and Hello World Program in Advance Java (J2EE)

In this tutorial how to create a new project, how to create Hello World Program and how to execute the project in Advance Java (J2EE) is shown.

To Create a new J2EE Project in Netbeans IDE, follow the below mentioned steps:

  1. Go to File Menu
  2. Select New Project
  3. A new Dialog Box will appear
  4. In the Categories Select “Java Web” and in Projects Select “Web Application”
  5. Then click on Next Button
  6. Write the Project Name
  7. Set the path of the project if required
  8. Then click on Next Button
  9. Now select a Server
  10. And click on Finish Button

To create a new Servlet

  1. Right Click on “Web Pages” Folder
  2. In New, select Servlet
  3. A new Dialog Box will appear
  4. Enter the Class name, it should be same as the action attribute in <form> tag
  5. Then click on Next Button
  6. Now Select the checkbox for Add information to deployment descriptor (web.xml)
  7. Click on Finish Button

Code:

index.html

<html>
<head>
<title>My first hello world program</title>
<meta charset=”UTF-8″>
<meta name=”viewport” content=”width=device-width, initial-scale=1.0″>
</head>
<body>
<form action=”HelloWorld” method=”POST”>
<input type=”submit” value=”Click Here”>
</form>
</body>
</html>

HelloWorld.java

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 doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType(“text/html;charset=UTF-8”);
try (PrintWriter out = response.getWriter()) {
/* TODO output your page here. You may use following sample code. */
out.println(“<!DOCTYPE html>”);
out.println(“<html>”);
out.println(“<head>”);
out.println(“<title>Servlet HelloWorld</title>”);
out.println(“</head>”);
out.println(“<body>”);
out.println(“<h1>” + “Hello World” + “</h1>”);
out.println(“</body>”);
out.println(“</html>”);
}
}

}

web.xml

<?xml version=”1.0″ encoding=”UTF-8″?>
<web-app version=”3.1″ xmlns=”http://xmlns.jcp.org/xml/ns/javaee” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation=”http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd”>
<servlet>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>HelloWorld</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorld</servlet-name>
<url-pattern>/HelloWorld</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>

 

https://youtu.be/NbCvHYd2cNA

Add a Comment

Your email address will not be published. Required fields are marked *