Sunday, November 6, 2016

Eclipse setup for JEE

1. Install apache server
2. Go to Eclipse->Windows-> Preferences->Server->Runtime Environment
3. Click Add
4. Select Apache server 7.0
5. Click next and download and install
6. Right click on the server. Go to properties. Then click on the Switch Location.
7. Double click on the Tomcat server. Select 'Use tomcat installation' instead of 'Use workspace metadata'.
8. Restart the apache server.

Thursday, January 21, 2016

Configuring Git in windows

1. Download Git and install
 
    https://git-scm.com/download/win

2. Add path to git to PATH environment variable.

    C:\Program Files\Git\cmd

Wednesday, January 13, 2016

JSP Elements

JSP Elements

JSP page is converted to Servlet java class. Therefore following elements in JSP will be copied in Servelet. Suppose there is a JSP page named "DisplayCounter.jsp". It will be converted to DisplayCounter_jsp class when it is placed in container. DisplayCounter_jsp will be extended from HttpServlet. Inside this class _jspService() method will be created and all the java code will be populated there.
  • Directives
    Page directive
    <%@ page import="java.util.*" %>
    Page directive defines page-spacific properties such as character encoding, the content type, whether this page should have the implicit session object.
    Attributes in page directives are
    • import
    • isThreadSafe
    • contentType
    • isELIgnored
    • isErrorPage
    • errorPage
    Taglib directive
    <%@ taglib tagdir="/WEB-INF/tags/cool" prefix="cool" %>
    It defines tag libraries available to the JSP.
    Include directive
    <%@ include file="wickedHeader.html" %>
    It defines text and code that gets added into the current page at translation time. This lets you build reusable chunks withou having to duplicate all that code in each JSP.
  • Declaration
    <%! int count=0; %>
    Declare count will be a member variable. You can define method or member variable.
  • Scriptlet
    <% out.println(count); %>
    Inside the scriptlet, you can write java code which will be copeid to converted servelet.
  • Expression
    <%=count+1 %>
  • Jsp Comment
    `<%-- --%>

JSP Implicit objects

API Implicit Object
JSPWriter-------------------out
HttpServletRequest------request
HttpServletResponse----response
HttpSession----------------session
ServletContext------------application
ServletConfig--------------config
Throwable------------------exception
PageContext--------------pageContext
Object-----------------------page

Attributes in a JSP

Application

application.setAttribute("username",user);

Request

request.setAttribute("username",user);

Session

session.setAttribute("username",user);

Page

pageContext.setAttribute("username",user);

Attributes can be accessed through PageContext

There are 4 PageContexts
APPLICATION_SCOPE
PAGE_SCOPE
REQUEST_SCOPE
SESSION_SCOPE
We can pass this in overloaded method of pageContext.setAttribute or pageContext.getAtribute as a 3rd parameter.
  • Accessing a page-scoped attribute
    <% pageContext.setAttribute("username",username); %>
    <%= pageContext.getAttribute("username",username); %>
  • Accessing a session-scoped attribute
    <% pageContext.setAttribute("username",username, PageContext.SESSION_SCOPE); %>
    <%= pageContext.getAttribute("username",username, PageContext.SESSION_SCOPE); %>
  • Accessing a application-scoped attribute
    <% pageContext.setAttribute("username",username, PageContext.APPLICATION_SCOPE); %>
    <%= pageContext.getAttribute("username",username, PageContext.APPLICATION_SCOPE); %>
  • Accessing a page-scoped attribute
    <% pageContext.setAttribute("username",username, PageContext.PAGE_SCOPE); %>
    <%= pageContext.getAttribute("username",username, PageContext.PAGE_SCOPE); %>
  • Accessing a request-scoped attribute
    <% pageContext.setAttribute("username",username, PageContext.REQUEST_SCOPE); %>
    <%= pageContext.getAttribute("username",username, PageContext.REQUEST_SCOPE); %>

Tuesday, January 12, 2016

How to use Maven

Advantages of using Maven project.

1. All the required jar are added.
2. It removes dependancy of one jar on another.
3. It helps to create project file structure.
4. It helps to build, publish and deploy the project.

How to setup Maven.

1. Download the latest binary zip archive from https://maven.apache.org/download.cgi.

2. Extract it.

3. Add following environment variables
M2_HOME=C:\Program Files\Apache Software Foundation\apache-maven-3.3.3
M2=%M2_HOME%\bin
MAVEN_OPTS=-Xms256m -Xmx512m
Add %M2%; to PATH environment variable

4. Now open the command prompt and run the following command.
   >mvn --version
If path is set ok, then you will see the output.


Creating a project.
1. create a directory
>mkdir myapp

2. mvn archetype:generate

Arctype is project structure
Group id is package name 
artifact id is Application name.
Version is version number
Package is what kind of packaging would be. like jar, war or ear.

When you select any structure it creates that file structure and pom.xml. In the pom.xml, dependancy jars are mentioned. Following command build project.

3. 
mvn package
mvn test
mvn compile
mvn build
mvn install
mvn clean

Suppose you added import org.sl4j.*; in your code.
Now if you compile the application (mvn compile), it will fail.
So you have to go to http://mvnrepository.com/ and search for slf4j. Click on the api link. Click on the version you like. Then copy the dependancy snippet and paste in pom.xml of the project under dependencies element. Then compile will be successful.

To add maven plugin in Eclipse:
help->Install->Work with (--All Available Sites--). Type maven. Select General Purpose Tools with m2e Maven Integration for Eclipse . Then install it.
You can do the same with Maven eclipse plugin. Create a Maven project. Then you can choose archetype. You can compile, build and everything.

Cookie

Server sends the cookie to client and client sends it back with each subsequent request. Session cookies vanishes when the client's browser quits, but it is possible to set set cookies lifetime.

Creating a new cookie

Cookie cookie=new Cookie("username","parvez");

Setting time to live

cookie.setMaxAge(30);

Sending cookie to client

response.addCookie(cookie);

Getting cookie from client's request

Cookie[] cookies=request.getCookies();
for(int i=0;i<cookies.length;i++
 {
  Cookie cookie=cookies[i];
  out.println(cookie.getName()+" "+cookie.getValue());
 }