163 subscribers
3.42K photos
24 videos
39 files
371 links
Link: @java_posts

Contact: java.response.email@gmail.com
Download Telegram
Now, our project is ready; the project structure will look as follows:
Step 3: Create a Servelet
Now, create a servlet by right-clicking on the Java Resources/src folder. To create a servlet right click on the src folder and navigate to the New-> Servlet menu. Here, provide the Servlet name:

Click on the Finish button. It will create a TestServlet as specified. You can choose any of your Servlet names.
import java.io.IOException;
import javax.servelet.ServeletException;
import javax.servelet.annotation.WebServelet;
import javax.servelet.HttpServelet;
import javax.servelet.http.HttpServeletRequest;
import javax.servelet.http.HttpServeletResponse;

/
* Servelet implementation class TestServelet
*/
@WebServelet("/TestServelet")
public class TestServelet extends HttpServlet {
private static final long serialVersionUID = 1L;

/

* @see HttpServelet#HttpServelet()
*/
public TestServelet() {
super()'
// TODO Aut-generated constructor stub
}
/
* @see HttpServelet#doGet(HttpServeletRequest request, HttpServeletResponse
*/
protected void doGet(HttpServeleyRequest request, HttpServeletResponse response) throws ServeletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
}

/

* @see HttpServelet#doPost(HttpServelet request, HttpServeletResponse respons)
*/
protected void doPost(HttpServeletRequest request, HttpServeletResponse response) throws ServeletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}

}
Step4: Add the Serveler jar file
We can see our servelet is displaying lots of errors it is because we have not added the servelet-api jar file yet. To add the jar. right-click on the project and select the configuration option by navigation to built path -> configure build path option. Now, click on the Add External JARs option.
Click Open to continue.

Now select Apply and Close option. It will add the jar file to our project.

Step5: Create a HTML or JSP file

Now, our first web application is almost ready. We can create HTML pages that we want to display on our website.

To create an HTML page, right-click on the WebContent folder and select the New HTML file option from the New-> HTML File menu with the name index.html.
<!DOCTYPE html>
<html>
<head>
<meta charset=<em>"UTF-8"</em>>
<title>First Web Application</title>
</head>
<body>
<h1>Welcome</h1>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">News</a></li>
<li><a href="#">Contact</a></li>
<li><a href="#">About</a></li>
</ul>
</body>
</html>
Step6: Map the File

Now, map this file in the web.xml file. The web.xml is a deployment descriptor for the Servlet applications. Since, Servlet 3.0, we can use annotations instead of the deployment descriptor.

To map a servlet, we have to provide the servlet details such as Servlet name and class. Consider the below code:
<servlet>
<servlet-name>MyHttpTestServlet</servlet-name>
<servlet-class>TestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyHttpTestServlet</servlet-name>
<url-pattern>/welcome</url-pattern>
</servlet-mapping>
We can also define our welcome file; a welcome file is the first file of the project that initiates the project, also known as Home. We can define multiple welcome files.

Consider the below code:
<?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">
<display-name>BasicWebApplication</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>MyHttpTestServlet</servlet-name>
<servlet-class>TestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyHttpTestServlet</servlet-name>
<url-pattern>/welcome</url-pattern>
</servlet-mapping>
</web-app>
From the above code, we can see by default the servlet defines several welcome files. If you want to use any file other than the listed files, you can define that here.

Now, our first web application is ready.

Step7: Run the Application

To run the application, right-click on the project and run it on the server by selecting Run-> Run on Server option.
It will take some time to load the application
The best Java GUI frameworks widely used by Java Developer Community for creating rich GUI based Java applications are

JavaFX
AWT
Apache Pivot
Swing and SwingX
SWT
Best Java GUI Frameworks
JavaFX

The latest flagship of Oracle is JavaFX and is counted at top among the Best Java GUI frameworks. JavaFX is the latest toolkit of Java/Oracle replacing Swing to create rich desktop as well as web applications. Many new Java GUI programs are being developed using JavaFX.
It produces a modern and rich look to its GUI’s when compared to the older Swing and AWT. It is also supported for Mobile apps. Any JavaFX program you write on desktop applications will also work fine on Android and iOS. JavaFX is nice and simple to use.
AWT
The Abstract Window Toolkit (AWT) can be called as very foundation of swing. AWT has optimized performance but still lacks some advanced features.

It is perfect for smaller a Java UI application that doesn’t require various rich user interfaces. This is being used for many years and has proven to be good for Full stack Java developers.

AWT supports Graphical User Interface programming. Some important features of AWT are:

Native user interface components.
Graphics and imaging tools for example shape, font classes.
A robust event-handling model.
Data transfer classes for easy cut-paste actions