Thursday, January 24, 2019

How To Run Jsp and Java Servlet Program in PC by Tomcat Server

hello


If You Want to Run Java Program then First you must need to Install Apache Tomcat Software Download or Xampp Software. This Software, are Provide a platform which we can create a own Directory with help of Local Server as Local Host.

Download Apache Tomcat
or Download Xammp

Apache Tomcat:-
Tomcat is an Application Sever, it Server Create by Apache Software Foundation that Executer Java Servlets and run HTML Web page with also JSP (Java Server Page) Coding Language.omcat can be used as either a standalone product with its own internal Web server or together with other Web servers, including Apache, Netscape Enterprise Server, Microsoft Personal Web Server and Microsoft Internet Information Server (IIS).

Xammp:-
It is a Free Open Source Cross Platform wev sever solution stack package, It's Developed by Apache Software Foundation.

Their homepage header reads "XAMPP Apache + MariaDB + PHP + Perl", indicating that this abbreviation is a recursive acronym.

XAMPP is regularly updated to the latest releases of Apache, MariaDB, PHP and Perl. It also comes with a number of other modules including OpenSSL, phpMyAdmin, MediaWiki, Joomla, WordPress and more.

(What is Local Host :- Local Host in a own domain where Directory are stored, which our Data file, class file, images, css file etc will be stored. in our computer we use Local host in run url path with any browser as "localhost:8080/codinggallery", then our Xammp Server will be Load.)


How To Run Java Program in PC by Tomcat Server

After the Install Apache Tomcat in your Device then Create a Directory Follow as:-

Structure for run jsp and java file
Step 1:- Create your Directory (Folder) In webapps Folder
Step 2:- Into your Directory Create a WEB-INF Folder
Step 3:- In WEB-INF Folder you can create JSP, HTML & JAVA Files.
Step 4:- Each Java files will be Class file which store in classes folder.

Hello Print Using JSP Program:-


<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
       <% out.println("Hello"); %>
    </body>
</html>


Hello Print Using Java Servlet Program:-

For The Run Java file you need three files:-
1. NewServlet.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 NewServlet extends HttpServlet {

    protected void doGet(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("Hello Servlet");

        }
    }


}


2. web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
      <servlet>
        <servlet-name>NewServlet</servlet-name>
        <servlet-class>NewServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>NewServlet</servlet-name>
        <url-pattern>/NewServlet</url-pattern>
    </servlet-mapping>
</web-app>


3. NewServlet.class
we need to each java file create class file, it file will be store in \web\WEB-INF\classes for this follow this steps:-
Step -1 :- Right Click + Shift
Step -2 :- Select "Open Powershell window here" Option. This task do in your Directory.
Step -3 :- Type javac (file name)
Example:- javac NewServlet.java

Step 4:- Type in Browser LocalHost:8080/website/Newservlet.java

Note:- Java file and Class file always File name should be same
Disqus Comments