Monday, May 26, 2014

Creating a sample dynamic web project without IDE

Creating a dynamic web project without IDE:

Step 1:

"WebProjectGenerator.bat"
Copy the below given code to notepad and save it as a batch file (WebProjectGenerator.bat )
----------------------------------------------------------------------------------------------------------------------------------
@ECHO OFF
::%USERNAME will pick the system user name.
ECHO ############################## Welcome %USERNAME% ##############################

::ECHO. will print a new line here.
ECHO.
::Get the project name from user as an input, declaring a 'Create' function
:Create
SET /P projectName=Please enter a new project name:
ECHO.

::Check if user provided project name is blank or not.If blank then error else create the project.
::Note- In batch programming we can use only 'IF' statement, it does'nt support 'IF-ELSE' statements.
IF "%projectName%"=="" ECHO You did not enter a project name! & GOTO Error
ECHO Hello, you have entered new project name as: '%projectName%'
ECHO.

::Check if user provided project name already exists or not.If exist then ask to provide the project name again
@ECHO off
IF EXIST %projectName% ECHO The name of project ('%projectName%') you have entered is already exists! & GOTO CreateAgain

SET metainf="META-INF"
SET webinf="WEB-INF"
SET indexjsp="index.jsp"
SET indexhtml="index.html"
SET webxml="web.xml"
SET lib="lib"
SET classes="classes"


ECHO Creating %projectName% ...
MKDIR %projectName%
CHDIR %projectName%

ECHO Creating %metainf% ...
MKDIR %metainf%

ECHO Creating %webinf% ...
MKDIR %webinf%

::^ Character is used to ignore checking of '<' and '>' symbols by batch compiler.
ECHO Creating %indexjsp% ...
ECHO ^<html^>^<head^>^<title^>Write your title of the page here^</title^>^</head^>^<body^>Write your code here...^</body^>^</html^> >> %indexjsp%

::^ Character is used to ignore checking of '<' and '>' symbols by batch compiler.
ECHO Creating %indexhtml% ...
ECHO  ^<html^>^<head^>^<title^>Write your title of the page here^</title^>^</head^>^<body^>Write your code here...^</body^>^</html^> >> %indexhtml%

CHDIR %webinf%

ECHO Creating %lib% ...
MKDIR %lib%

ECHO Creating %classes% ...
MKDIR %classes%

ECHO Creating %webxml% ...
ECHO ^<web-app^>^<display-name^>Name of your project^</display-name^>^<description^>Description about your project^</description^>^<welcome-file-list^>^<welcome-file^>index.html^</welcome-file^>^<welcome-file^>index.jsp^</welcome-file^>^</welcome-file-list^>^</web-app^> >> %webxml%

CD ../../

ECHO Copying ServletCompilerHelper file ...
COPY ServletCompilerHelper.bat %projectName%\%webinf%\%classes%\

GOTO Success

::Declaring a 'Error' function
:Error
ECHO.
SET /P flag=Do you want to create new project (Y/N)? or type 'exit' to exit from here:
::These multiple if statements represents OR conditions, In batch programming this is the way to check "OR" condition.
IF "%flag%"=="exit" ECHO. & GOTO EXIT 
IF "%flag%"=="EXIT" ECHO. & GOTO EXIT   
IF "%flag%"=="" ECHO Entered option is invalid! & GOTO End  
IF "%flag%"=="Y" ECHO. & GOTO Create   
IF "%flag%"=="y" ECHO. & GOTO Create  
IF "%flag%"=="N" ECHO. & GOTO End  
IF "%flag%"=="n" ECHO. & GOTO End  
IF NOT "%flag%"=="Y" ECHO Entered option is invalid, Please select (Y/N)! & GOTO Error 
IF NOT "%flag%"=="y" ECHO Entered option is invalid, Please select (Y/N)! & GOTO Error   
IF NOT "%flag%"=="N" ECHO Entered option is invalid, Please select (Y/N)! & GOTO Error  
IF NOT "%flag%"=="n" ECHO Entered option is invalid, Please select (Y/N)! & GOTO Error   

::Declaring a 'CreateAgain' function
:CreateAgain
ECHO.
SET /P createflag=Do you want to create new project (Y/N)? or type 'exit' to exit from here:
::These multiple if statements represents OR conditions, In batch programming this is the way to check "OR" condition.
IF "%createflag%"=="exit" ECHO. & GOTO EXIT 
IF "%createflag%"=="EXIT" ECHO. & GOTO EXIT   
IF "%createflag%"=="" ECHO Entered option is invalid! & GOTO End  
IF "%createflag%"=="Y" ECHO. & GOTO Create  
IF "%createflag%"=="y" ECHO. & GOTO Create  
IF "%createflag%"=="N" ECHO. & GOTO End  
IF "%createflag%"=="n" ECHO. & GOTO End  
IF NOT "%createflag%"=="Y" ECHO Entered option is invalid, Please select (Y/N)! & GOTO Error 
IF NOT "%createflag%"=="y" ECHO Entered option is invalid, Please select (Y/N)! & GOTO Error   
IF NOT "%createflag%"=="N" ECHO Entered option is invalid, Please select (Y/N)! & GOTO Error  
IF NOT "%createflag%"=="n" ECHO Entered option is invalid, Please select (Y/N)! & GOTO Error

::Declaring a 'Success' function
:Success
ECHO.
ECHO Directory structure of your project is ready,configure it as per your need, Thanks!!

::Declaring a 'End' function
:End
ECHO.
ECHO Bye Bye!!

::Declaring a 'EXIT' function
:EXIT
ECHO.
ECHO Exiting the command prompt, Bye Bye!!

EXIT
----------------------------------------------------------------------------------------------------------------------------------
Step 2:

"ServletCompilerHelper.bat"

Copy the below given code to notepad and save it as a batch file (ServletCompilerHelper.bat )

---------------------------------------------------------------------------------------------------------------------------------
@echo off
echo ############################### Welcome, %USERNAME% ###############################

:: Command to insert a line break on the screen is>> 'echo.'
echo.

echo Setting java path...
:: Command to set the bin path of jdk set in environment variables is>> 'set PATH=%JAVA_HOME%\bin;%PATH%'
set PATH=%JAVA_HOME%\bin;%PATH%
echo Java path is set successfully!
echo.
echo Below is the version of jdk installed in your machine:::::::
:: Command to display java version installed in your machine>> 'java -version'
java -version
echo.

:CompileJavaCode
SET /P flag=Do you want to compile java source code (Y/N)?
::These multiple if statements represents OR conditions, In batch programming this is the way to check "OR" condition.
IF "%flag%"=="" ECHO. & GOTO End   
IF "%flag%"=="Y" ECHO. & GOTO Compile  
IF "%flag%"=="y" ECHO. & GOTO Compile  
IF "%flag%"=="N" ECHO. & GOTO End  
IF "%flag%"=="n" ECHO. & GOTO End  
IF NOT "%flag%"=="Y" ECHO Entered option is invalid & GOTO End 
IF NOT "%flag%"=="y" ECHO Entered option is invalid & GOTO End
IF NOT "%flag%"=="N" ECHO Entered option is invalid & GOTO End
IF NOT "%flag%"=="n" ECHO Entered option is invalid & GOTO End

::Declaring a 'Compile' function
:Compile
javac -classpath %CATALINA_HOME%\lib\servlet-api.jar *.java

::Declaring a 'End' function
:End
ECHO.

ECHO Bye Bye!! 

*********************************************************************************

Step 3:

Put WebProjectGenerator.bat and ServletCompilerHelper.bat files in webapps directory/folder of Apache tomcat.

Step 4:
Run WebProjectGenerator.bat file and follow the instructions. It will create the sample web project based on the given inputs.

Step 4:
To compile the class files i.e. Java Servlets use ServletCompilerHelper.bat file. It will compile all the servlet source files into class files.

No comments:

Post a Comment

Thanks for your comments/Suggestions.