Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

How to solve The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path problem in Eclipse

In this post I'm going to show how to solve The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path problem in Eclipse.

Problem:
After you create a dynamic project in Eclipse. You go on and try to create JSP page and you see a problem like this:


Error Message :

superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path problem in Eclipse


Solution:

1. Go to Properties by right clicking on the project you are getting error.


2. Click on Project Facets


3. Check Apache Tomcat Option in Runtime tab.



 4. Click Apply and Close

Hola!!! Error is gone.


That's it you have solved the issue now.

JSP scripting element Example | Scriptlet Tag vs Declaration Tag vs Expression Tag | Example

In this post I am going to post an example depicting the use of JSP scripting Elements.


Scriptlet Tag vs Declaration Tag vs Expression Tag | Example

Overview

Scriptlet Tag
Scriptlet Tag is used to execute Java Code in JSP.

Expression Tag
It is mainly used to print the value of the variable or method. We don't need to use out.print.

Declaration Tag
it is used to declare variables and methods.

Output of this Example:



Welcome


Square



 index.jsp Code


<%@page import="java.util.Calendar"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>JSP Exmaples</title>
</head>
<body>
<h1 align="center">
<%
out.println("Welcom to JSP Page");
%>

</h1>
<hr>
<h2>Scriptlet Tag is used to execute Java Code in JSP</h2>
<h3>Example</h3>

<hr>

<form action="welcome.jsp">
<input type="text" name="user_name"> <input type="submit"
value="Submit">

</form>

<hr>

<h2>Expression Tag</h2>
<p>It is mainly used to print the value of the variable or method.
We don't need to use out.print.</p>
<h3>Example</h3>

Current Time is
<%=Calendar.getInstance().getTime()%>

<hr>

<h2>Declaration Tag</h2>

<p>it is used to declare variables and methods.</p>

<h3>Example</h3>

<form action="square.jsp">
<label> Enter a number to find square</label>

<input type="number" name="number">
<input type="submit" value="Get The Square">
</form>
<hr>
</body>
</html>


welcome.jsp Code



<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Welcome</title>
</head>
<body>

<% String name=request.getParameter("user_name");

out.println("Welcome "+name);
%>

</body>
</html>



square.jsp Code



<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>

<%
String n = request.getParameter("number");
int num = Integer.parseInt(n);
%>
<meta charset="ISO-8859-1">
<title>Square of <%= num %></title>
</head>
<body>

<%!
int square(int num) {
return num*num;
}

%>

<h2> The Square of <%= num %> is <%= square(num) %></h2>

</body>
</html>


If you have issues regarding how to create JSP page in Eclipse with Tomcat follow this post

Create First JSP

Create First JSP Program using Eclipse IDE and Tomcat Server

In this post I am going to show how to create First JSP Program using Eclipse IDE and Tomcat Server.

Final Output we have to achieve:

We will follow following 9 steps to create our first JSP Program using Eclipse and Tomcat.


1. Open Eclipse IDE in J2EE perspective.


Open Eclipse in your work space.



2. Create a Dynamic Web Project from File menu.




Select, menu File --> New --> Dynamic Web Project.


3. Enter "HelloJSP" as the project name.




Keep rest of the settings as shown below.

Click Next


4. Click "Next" button.






5. Check 'Generate web.xml deployment descriptor


Click on checkbox and click "Finish" button and Eclipse IDE will generate the web project automatically as shown in Screenshot



6.Create Jsp page


Right click on 'WebContent' folder and select from context menu New --> Jsp File.



7. Name "index.jsp"


Give the name of the file as "index.jsp" in the 'File Name' field and Click "Finish" button.



8. Write JSP Code


Edit the generated 'helloWorld.jsp' as per the following code.



9. Run Your Code

Right click on 'helloWorld.jsp' and select from context menu 'Run As' --> 'Run on Server'.


As we followed all steps now, we have successfully achieved our output page.

List Directory and files in Java

import java.io.File;
//list directory and files in java
public class ListFilesUtil {
    /**
     * List all the files and folders from a directory
     * @param directoryName to be listed
     */
    public void listFilesAndFolders(String directoryName){
        File directory = new File(directoryName);
        //get all the files from a directory
        File[] fList = directory.listFiles();
        for (File file : fList){
            System.out.println(file.getName());
        }
    }
    /**
     * List all the files under a directory
     * @param directoryName to be listed
     */
    public void listFiles(String directoryName){
        File directory = new File(directoryName);
        //get all the files from a directory
        File[] fList = directory.listFiles();
        for (File file : fList){
            if (file.isFile()){
                System.out.println(file.getName());
            }
        }
    }
    /**
     * List all the folder under a directory
     * @param directoryName to be listed
     */
    public void listFolders(String directoryName){
        File directory = new File(directoryName);
        //get all the files from a directory
        File[] fList = directory.listFiles();
        for (File file : fList){
            if (file.isDirectory()){
                System.out.println(file.getName());
            }
        }
    }
    /**
     * List all files from a directory and its subdirectories
     * @param directoryName to be listed
     */
    public void listFilesAndFilesSubDirectories(String directoryName){
        File directory = new File(directoryName);
        //get all the files from a directory
        File[] fList = directory.listFiles();
        for (File file : fList){
            if (file.isFile()){
                System.out.println(file.getAbsolutePath());
            } else if (file.isDirectory()){
                listFilesAndFilesSubDirectories(file.getAbsolutePath());
            }
        }
    }
    public static void main (String[] args){
        ListFilesUtil listFilesUtil = new ListFilesUtil();
        final String directoryLinuxMac ="/Users/loiane/test";
        //Windows directory example
        final String directoryWindows ="C://test";
        listFilesUtil.listFiles(directoryLinuxMac);
    }
}