Configuration for advance java

Three steps to configure advance java –
1-Installation of server in windows

2-Configuration of server in eclipse

3-Web project creation in eclipse

1-Installation of server in windows(Tomcat)

step 1- check for latest server adopter available in eclipse

tom1

step 2-Goto http://tomcat.apache.org ⇒ Downloads ⇒ Tomcat 8.0.{xx} Continue reading “Configuration for advance java”
Advertisements

Step1: Servlet Definition

Servlet definition-

It’s a java program that runs on a server.
Basically, servlet is a server side technology,
-it handles incoming requests from client
-provides response to the server.

Step 3: Servlet creation

servlet-hierarchy

Ways to create servlet-

1- implement servlet interface-
Servlet interface has total 5 methods
-3 are life cycle methods
(i)init()
(ii)service()
(iii)destroy()

2 are non- life cycle methods-
(i)getServletConfig
(ii)getServletInfo

2- extend GenericSevlet abstract class-

i– it’s a abstract class,
it implements servlet interface,
it has abstract service method

ii– it’s protocol independent.

iii– it belongs to javax.servlet package.

3- extend HttpServlet abstract class-

i– extends genericSerrvlet class and provides implementation of service methods

ii– No abstract methods,
but assigned as abstract so user can’t create object and should be created by server.

iii– it belongs to javax.servlet.http package

Best way to create servlets-> 3- extend HttpServlet abstract class-

Step5:Working process of servlet + code

7 Step of working process of servlet-

step 1-client request to server for any servlet.

step 2-server finds that servlet using servlet-mapping and create 2 objects,
(a)request object- for servlet
(b)response object-for client

step 3- server calls service method in new thread and pass request and response objects as argument.

step 4-service method calls doPost() if request from client is POST else doGet().

step 5- servlet provide output in response object to server.

step 6-Server passes response object to client.

step 7- thread dies, garbage collection.

-Block Diagram

wrksrvlt

 

Example 1-Web app to call a servlet to print “java is simple” from a html page.

Output-ezgif-com-ef2aa71955

 

Project Structure-

we123

-3 Files-
1-XYZ.java(inside package ‘abc’)
2-web.xml(inside WEB-INF folder)
3-index.html(inside WebContent folder)

1-XYZ.java (servlet)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package abc;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class XYZ extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.print("java is simple");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}

 

2-web.xml (deployment descriptor)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?xml version="1.0" encoding="UTF-8"?>
<display-name>FirstWeb</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>a</servlet-name>
<servlet-class>abc.XYZ</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>a</servlet-name>
<url-pattern>/pqr</url-pattern>
</servlet-mapping>
</web-app>

3-index.html

1
2
3
4
5
6
7
<html>
<body>
<a href="pqr">click here</a>
</body>
</html>

 

Step6: Servlet collaboration and communication + code

1- Request Dispatcher Interface-
Definition-RequestDispatcher is used to dispatch request to any resources on the server.
Syntax-

1
RequestDispatcher rs = request.getRequestDispatcher("resource name");

– Methods of RequestDispatcher
(i)-forward method-
Syntax-

1
void forward(ServletRequest request,ServletResponse response)

-This method forwards a request from a servlet to another resource (servlet, JSP file, or HTML file) on the server.

Example 2- Web app to create a servlet which sends request object to another servlet using forward method.
Output-

oD.gif

Project Structure-

we.png

4 files-

1-Xy.java (inside package ‘abc’)
2-Nm.java(inside package ‘abc’)
2-web.xml(inside WEB-INF folder)
3-index.html(inside WebContent folder)

1.Xy.java(servlet)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package ab;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Xy extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
RequestDispatcher rd=request.getRequestDispatcher("second");
rd.forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}

2.Nm.java(servlet)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package ab;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Nm extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out=response.getWriter();
out.print("i am Nm servlet");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}

3.web.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?xml version="1.0" encoding="UTF-8"?>
<display-name>ForwrdExample</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>q</servlet-name>
<servlet-class>ab.Xy</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>q</servlet-name>
<url-pattern>/first</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>w</servlet-name>
<servlet-class>ab.Nm</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>w</servlet-name>
<url-pattern>/second</url-pattern>
</servlet-mapping>
</web-app>

4.index.html

1
2
3
4
5
6
7
8
9
10
11
12
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<a href="first">click here</a>
</body>
</html>

[]

2-Include method-
Syntax-

1
void include(ServletRequest request, ServletResponse response)

-This method includes the content of a resource (servlet, JSP page, HTML file) in the response.

Example 3- Web app to create a servlet which includes data of html page into a servlet using include method.
Output-

asdasd

Project Structure

unti

3 files-

1-Yz.java (inside package ‘ab’)
2-web.xml(inside WEB-INF folder)
3-index.html(inside WebContent folder)

1-Yz.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package ab;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Yz extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out =response.getWriter();
RequestDispatcher rd=request.getRequestDispatcher("index.html");
rd.include(request, response);
out.print("this is from Yz servlet");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}

2-web.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?xml version="1.0" encoding="UTF-8"?>
<display-name>IncludExampl</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>a</servlet-name>
<servlet-class>ab.Yz</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>a</servlet-name>
<url-pattern>/go</url-pattern>
</servlet-mapping>
</web-app>

3-index.html

1
2
3
4
5
6
7
8
9
10
11
12
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<a href="go">click here</a>
</body>
</html>

2-SendRedirect-
This method redirects the response to another resource.
Syntax-

1
response.sendRedirect("resource name");

Example 4- Web app to create a servlet which executes another servlet using sendRedirect method.
Output-sas

Project Structure-

snsp

4 files-

1-Qw.java (inside package ‘ab’)
2-Zx.java(inside package ‘ab’)
3-web.xml(inside WEB-INF folder)
4-index.html(inside WebContent folder)

1.Qw.java(servlet)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package ab;
import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.*;
public class Qw extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.sendRedirect("second");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}

2-Zx.java(servlet)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package ab;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Zx extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("this is Zx servlet");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}

3-web.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?xml version="1.0" encoding="UTF-8"?>
<display-name>SendrdrctExampl</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>q</servlet-name>
<servlet-class>ab.Qw</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>q</servlet-name>
<url-pattern>/first</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>w</servlet-name>
<servlet-class>ab.Zx</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>w</servlet-name>
<url-pattern>/second</url-pattern>
</servlet-mapping>
</web-app>

4-index.html

1
2
3
4
5
6
7
8
9
10
11
12
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<a href="first">click here</a>
</body>
</html>

#Difference between sendRedirect method and methods of Request Dispatcher interface(forward and include)#

1-sendRedirect() makes the client(browser) create a new request to get to the resource, the user can see the new URL

2-RequestDispatcher get the resource in same request and URL does not changes.

3-sendRedirect() works on response object while RequestDispatcher works on request object.

Step7: ServletConfig and ServletContext + code

ServletConfig interface-

Definition- Servlet config is used to pass information to a servlet during initialization.

Step 1- Initialization of key-value pair in web.xml for particular servlet

Step 2-Access value using key in servlet

Example 5- Web app to create a servlet which gets data from web.xml using ServletConfig interface.

Output-cnf

Project Structure-

cnfg2

3 files-

1-Zx.java(inside package=’ab’)

2-web.xml(inside WEB-INF folder)

3-index.html(inside WebContent folder)

1-Zx.java(servlet)-

package ab;

import java.io.*;

import javax.servlet.*;
import javax.servlet.http.*;

public class Zx extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

ServletConfig sc=getServletConfig();
String a=sc.getInitParameter("myName");

PrintWriter out=response.getWriter();
out.print(a);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

doGet(request, response);
}

}

2-web.xml

<?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" version="3.1">
<display-name>Exconfig</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>q</servlet-name>
<servlet-class>ab.Zx</servlet-class>
<init-param>
<param-name>myName</param-name>
<param-value>Mr.Java</param-value>
</init-param>
</servlet>

<servlet-mapping>
<servlet-name>q</servlet-name>
<url-pattern>/first</url-pattern>
</servlet-mapping>

</web-app>

3-index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<a href="first">Click here</a>

</body>
</html>

ServletContext Interface- 

Definition-Used to get configuration information from Deployment Descriptor(web.xml).

-Available for all servlets or JSPs of the Web app.

Step 1- Initialization of key-value pair in web.xml for all servlets

Step 2-Access value using key in any servlet

 

Example 6- Web app to create any number of servlets which get data from web.xml using ServletContext interface.

Output-asd31

Project Structure-

xcn

4 files-

1-Yz.java(inside package= ‘ab’)

2-Zx.java(inside package=’ab’)

3-web.xml(inside WEB-INF)

4-index.html(inside WebContent)

1-Yz.java(Servlet)

package ab;

import java.io.*;

import javax.servlet.*;
import javax.servlet.http.*;

public class Yz extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

ServletContext sc = getServletContext();
String a= sc.getInitParameter("MyName");

PrintWriter out=response.getWriter();
out.print(a);
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

doGet(request, response);
}

}

2-Zx.java(servlet)

package ab;

import java.io.*;

import javax.servlet.*;
import javax.servlet.http.*;

public class Zx extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

ServletContext sc = getServletContext();
String a= sc.getInitParameter("MyName");

PrintWriter out=response.getWriter();
out.print(a);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

doGet(request, response);
}

}

3-web.xml

<?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" version="3.1">
<display-name>ExCntxt</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>

<context-param>
<param-name>MyName</param-name>
<param-value>Mr.Java</param-value>
</context-param>

<servlet>
<servlet-name>a</servlet-name>
<servlet-class>ab.Yz</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>a</servlet-name>
<url-pattern>/first</url-pattern>
</servlet-mapping>

<servlet>
<servlet-name>b</servlet-name>
<servlet-class>ab.Zx</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>b</servlet-name>
<url-pattern>/second</url-pattern>
</servlet-mapping>

</web-app>

4-index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<a href="first">click here servlet1</a>
<a href="second">click here servlet2</a>

</body>
</html>

Step8- Session Management + code

Basic concept of session- store some data of client temporarily which should be available until client destroy it.

Session management definition- It’s a mechanism used by web container to save state of client.

Session management includes-
1- session (interface)
2- cookies(class)
3- url rewriting
4- Hidden form field

1-Session-
step 1- creation of sessions-

(i)HttpSession ses=request.getsession();

-Returns existing session object else creates new session object.

(ii)HttpSession ses=request.getsession(true);

-Always returns existing session object.

(iii)HttpSession ses=request.getsession(false);

-Always creates new session object.

step 2- Initialization of servlet-

ses.setAttribute(key, value);

Note- Remove Attribute-
ses.removeAttribute(key);

step 3- Access of session-

ses.getAttribute(key);

4- Deletion of session-

ses.invalidate ();

Example 7- Wep app to create session at correct login, get session data from at profile page, and then logout to delete session.

Output-ezgif-com-3239aa70ad

Project Structure-

prjstr

6 files-

1-index.html(inside WebContent)

2-Login.java(inside pacage=’ab’)

3-Profile.java(inside pacage=’ab’)

4-profile.html(inside WebContent)

5-Logout.java(inside pacage=’ab’)

6-web.xml(inside WEB-INF)

1-index.html


<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<form action="login">

<label>email</label><input name="e">
<label>password</label><input name="p">
<input type="submit">
<a href="profile">View Profile</a>
</form>

</body>
</html>

2-Login.java(servlet)


package ab;

import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.*;

public class Login extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

String E1="mrjava@gmail.com";
String P1="java123";

String E2=request.getParameter("e");
String P2=request.getParameter("p");

if(E1.equals(E2)&amp;amp;amp;&amp;amp;amp;P1.equals(P2)){

HttpSession ses=request.getSession();
ses.setAttribute("mail", E2);
response.sendRedirect("profile");

}else{
response.sendRedirect("index.html");
}

}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

doGet(request, response);
}

}

3-Profile.java(servlet)


package ab;

import java.io.*;

import javax.servlet.*;
import javax.servlet.http.*;

public class Profile extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

PrintWriter out=response.getWriter();
HttpSession ses=request.getSession();
String m=(String) ses.getAttribute("mail");

if(m!=null){
response.sendRedirect("profile.html");
}else{
RequestDispatcher rd=request.getRequestDispatcher("index.html");
rd.include(request, response);
out.println("You are not logged in.");
}
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}

}

4-profile.html


<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

Welcome user
<a href="logout">Click here for logout</a>

</body>
</html>

5-Logout.java(servlet)


package ab;

import java.io.*;
import javax.servlet.http.*;

public class Logout extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

HttpSession ses=request.getSession();
ses.invalidate();
response.sendRedirect("index.html");
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}

}

6-web.xml


<?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" version="3.1">
<display-name>ExSession</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>a</servlet-name>
<servlet-class>ab.Login</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>a</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>

<servlet>
<servlet-name>b</servlet-name>
<servlet-class>ab.Profile</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>b</servlet-name>
<url-pattern>/profile</url-pattern>
</servlet-mapping>

<servlet>
<servlet-name>c</servlet-name>
<servlet-class>ab.Logout</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>c</servlet-name>
<url-pattern>/logout</url-pattern>
</servlet-mapping>

</web-app>

2- Cookies-

-cookie is a class.
-It stores textual data at client side.
-Less secure than session.

step 1- creation of cookie-
Cookie ck= new cookie(key, value);

step 2- send to the client side(browser)-
response. addCookie(ck);

step 3- access the cookie-
Cookie[] c= request.getCookies();

step 4- deletion of cookie-
ck.setAge(0);
response.addCookie(ck);

Example 8- Wep app to create cookie and add into browser at correct login, get cookies data from at profile page, and then logout to delete cookie.

output-ckks

Project Structure-

cks

6 files-

1-index.html(inside WebContent)

2-Login.java(inside pacage=’ab’)

3-Profile.java(inside pacage=’ab’)

4-profile.html(inside WebContent)

5-Logout.java(inside pacage=’ab’)

6-web.xml(inside WEB-INF)

1-index.html


<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<form action="login">

<label>email</label><input name="e">
<label>password</label><input name="p">
<input type="submit">
<a href="profile">View Profile</a>
</form>

</body>
</html>

2-Login.java(servlet)


package ab;

import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.*;

public class Login extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 String E1="mrjava@gmail.com";
 String P1="java123";

 String E2=request.getParameter("e");
 String P2=request.getParameter("p");

 if(E1.equals(E2)&amp;&amp;P1.equals(P2)){

 Cookie ck=new Cookie("mail", E2);
 response.addCookie(ck);
 response.sendRedirect("profile");

 }else{
 response.sendRedirect("index.html");
 }

 }

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

doGet(request, response);
}

}

3-Profile.java(servlet)


package ab;

import java.io.*;

import javax.servlet.*;
import javax.servlet.http.*;

public class Profile extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 PrintWriter out=response.getWriter();

 Cookie[] ck2=request.getCookies();
 System.out.println(ck2);

 if(ck2!=null){
 response.sendRedirect("profile.html");
 }else{
 RequestDispatcher rd=request.getRequestDispatcher("index.html");
 rd.include(request, response);
 out.println("You are not logged in.");
 }
 }

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}

}

4-profile.html


<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

Welcome user
<a href="logout">Click here for logout</a>

</body>
</html>

5-Logout.java(servlet)


package ab;

import java.io.*;
import javax.servlet.http.*;

public class Logout extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

 Cookie[] ck2=request.getCookies();
 for (Cookie c : ck2) {
 c.setMaxAge(0);
 response.addCookie(c);
 }
 response.sendRedirect("index.html");
 }

6-web.xml


<?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" version="3.1">
<display-name>ExCookies</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>a</servlet-name>
<servlet-class>ab.Login</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>a</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>

<servlet>
<servlet-name>b</servlet-name>
<servlet-class>ab.Profile</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>b</servlet-name>
<url-pattern>/profile</url-pattern>
</servlet-mapping>

<servlet>
<servlet-name>c</servlet-name>
<servlet-class>ab.Logout</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>c</servlet-name>
<url-pattern>/logout</url-pattern>
</servlet-mapping>
</web-app>

3- Url Rewriting-
Step 1- add “?” At the end of url
Step 2 -add data in following format-
Attribute=value
Step 3- separate these attribute and value with “&” symbol

Example 9- Web app to add data with url and get it on next servlet.

output-

uul

Project Structure-

urrl

3 files-

1-index.html(inside WebContent)

2-A.java(inside pacage=’ab’)

3-web.xml(inside WEB-INF)

1-index.html


<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<a href="first?name=mr.Java&email=a@a.com">Click here</a>

</body>
</html>

2-A.java(servlet)


package ab;

import java.io.*;

import javax.servlet.*;
import javax.servlet.http.*;
public class A extends HttpServlet {
private static final long serialVersionUID = 1L;

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String s1=request.getParameter("name");
String s2=request.getParameter("email");

PrintWriter out=response.getWriter();
out.println(s1);
out.println(s2);
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}

}

 

3-web.xml


<?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" version="3.1">
<display-name>ExUrlWr</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>z</servlet-name>
<servlet-class>ab.A</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>z</servlet-name>
<url-pattern>/first</url-pattern>
</servlet-mapping>
</web-app>

4- hidden form field-
Step1- create input box with type=hidden
Step2- assign it’s value
Step3- get value of input box on next servlet.

Example 10- Wep app get data from hidden input box on next servlet.

output-

hidd

Project Structure-

exhid

3 files-

1-index.html(inside WebContent)

2-A.java(inside pacage=’ab’)

3-web.xml(inside WEB-INF)

1-index.html


<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<form action="first">
<input name="a">

<input name="b">

<input type="hidden" name="c" value="Mr.java">
<input type="submit">
</form>

</body>
</html>

2-A.java(servlet)


package ab;

import java.io.*;

import javax.servlet.*;
import javax.servlet.http.*;
public class A extends HttpServlet {
private static final long serialVersionUID = 1L;

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

String s1=request.getParameter("a");
String s2=request.getParameter("b");
String s3=request.getParameter("c");

PrintWriter out=response.getWriter();
out.println(s1);
out.println(s2);
out.println(s3);

}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}

}

3-web.xml


<?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" version="3.1">
<display-name>ExHidden</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>a</servlet-name>
<servlet-class>ab.A</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>a</servlet-name>
<url-pattern>/first</url-pattern>
</servlet-mapping>
</web-app>

 

Step 9: Filters

Filter is used for pre-processing of requests and post-processing of responses.

Filter Api- it’s a part of servlet api, filter interface can be found in javax.servlet package.

Life cycle-
1- init
2- dofilter
3- destroy

Filterchain interface- its used to invoke other filters in the chain.

Example 11- Web app to show pre-processing text before request is handled by servlet and post-processing text after response is generated by servlet.

output-

zxc

Project Structure-

mlp

3 files-

1-index.html(inside WebContent)

2-Firstfil.java(inside pacage=’ab’)

3-FirstServ.java(inside pacage=’ab’)

4-SecFil.java(inside pacage=’ab’)

5-web.xml(inside WEB-INF)

1-index.html


<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="FirstServ">
<input name="name">
<input type="submit" value="ok">
</form>
</body>
</html>

2-Firstfil.java(filter)


package ab;

import java.io.*;
import javax.servlet.*;

public class Firstfil implements Filter {

public void destroy() {
// TODO Auto-generated method stub
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
String fs=request.getParameter("name");
System.out.println("first filter "+fs);
chain.doFilter(request, response);
System.out.println("after first filter chain-dofilter");
}
public void init(FilterConfig fConfig) throws ServletException {
// TODO Auto-generated method stub
}

}

3-FirstServ.java(servlet)


package ab;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class FirstServ extends HttpServlet {
private static final long serialVersionUID = 1L;

public FirstServ() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

String fs=request.getParameter("name");
response.getWriter().print(fs);
System.out.println("first servlet "+fs);
// pass the request along the filter chain
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}

}

4-SecFil.java(filter)


package ab;

import java.io.*;
import javax.servlet.*;

public class SecFil implements Filter {

public void destroy() {
// TODO Auto-generated method stub
}

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
// TODO Auto-generated method stub
String fs=request.getParameter("name");
System.out.println("second filter "+fs);
// pass the request along the filter chain
chain.doFilter(request, response);
System.out.println("after second filter chain-dofilter");
}

public void init(FilterConfig fConfig) throws ServletException {
// TODO Auto-generated method stub
}

}

5-web.xml


<?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" version="3.1">
<display-name>Filters</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>
<filter>
<filter-name>a</filter-name>
<filter-class>ab.Firstfil</filter-class>
</filter>
<filter-mapping>
<filter-name>a</filter-name>
<url-pattern>/FirstServ</url-pattern>
</filter-mapping>

<servlet>
<servlet-name>b</servlet-name>
<servlet-class>ab.FirstServ</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>b</servlet-name>
<url-pattern>/FirstServ</url-pattern>
</servlet-mapping>

<filter>
<filter-name>c</filter-name>
<filter-class>ab.SecFil</filter-class>
</filter>
<filter-mapping>
<filter-name>c</filter-name>
<url-pattern>/FirstServ</url-pattern>
</filter-mapping>
</web-app>

Step 10-JSP Definition

JSP- Java Server Pages

It’s used to create dynamic web applications like servlets, but it’s easier to maintain.

These are internally converted into servlets by server.

Jsp includes both html and jsp tags.
Html tags provides static output and jsp tags are provide dynamic output.

Step 14- JSP DIRECTIVES

1- page directives


<%@ page language="java"%>
<!-- scripting language-->
<%@ page autoFlush="true" %>
<!-- throw exception when false, on jspwriter's object -->
<%@ page buffer="8kb" %>
<!-- jspwriter buffer -->
<%@ page contentType="text/html;" %>
<!-- content representation -->
<%@ page errorPage="err.jsp" %>
<!-- error page -->
<%@ page extends="as.Aop" %>
<!-- extends on HttpServlet class, used rare -->
<%@ page import="java.util.ArrayList" %>
<!-- import -->
<%@ page info="new_Page" %>
<!-- can use it in code, get servletinfo -->
<%@ page isELIgnored="false" %>
<!-- expression language -->
<%@ page isErrorPage="false" %>
<!-- true for err.jsp -->
<%@ page isThreadSafe="true" %>
<!-- shouldn't be false because if it'll be singlethread, it'll lose all benefits -->
<%@ page pageEncoding="ISO-8859-1" %>
<!-- could be any encoding, like ascii -->
<%@ page session="true" %>
<!-- auto session creation -->
<%@ page trimDirectiveWhitespaces="false" %>
<!-- good for code space management -->
<%@ page deferredSyntaxAllowedAsLiteral="false" %>
<!-- characters are allowed as String literals -->

2- include directives


<%@ include file="here"%>
<!-- jsp,html,text file -->

3- taglib directives

<%@ taglib prefix="fmt"  uri="http://java.sun.com/jsp/jstl/core" %>
<!-- other prefixes are c,fmt,sql,x,fn for that particular uri -->

 

Step 15- JSP IMPLICIT OBJECTS

Object                                 Class

1-out –                                             Jspwriter (equivalent to PrintWriter) class

2-request –                                    HttpServletRequest class

3-response –                                  HttpServletResponse class

4- session –                                    Httpsession class

5-application –                              ServletContext class

6-exception –                                 Exception class

7-page –                                            It’s a synonym for “this” keyword of core java

8-pagecontext –                             Encapsulates use of server specific features

9- config –                                         ServletConfig class

Step 16- JSP Action Tags

1- include – runtime inclusion

<jsp:include page=”PageDIR.jsp” flush=”false”></jsp:include>

2- useBean – finds or instantiates a javaBean

<jsp:useBean id=”d1″ beanName=”s2″ scope=”page” type=”java.util.AbstractList”></jsp:useBean>

3- setProperty – sets the property of javaBean

<jsp:setProperty property=”name1″ name=”deepu” param=”p1″ value=”po1″/>

4- *2 getProperty – inserts property of javaBean into output

<jsp:getProperty property=”name1″ name=”peedu” />

5- forward – forward to new page

<jsp:forward page=”a.jsp”></jsp:forward>

6- param – used with forward to add data

<jsp:param value=”a1″ name=”a1a1″/>

7- *4 params- used witb forward to add multiple param data

<jsp:params  ></jsp:params>

8- Element – define xml elements dynamically

<jsp:element name=”dep” ></jsp:element>

9- Attribute – define xml element’s attribute

<jsp:attribute name=”deep” trim=”true”/>

10- body – define xml element’s body

<jsp:body></jsp:body>

11- fallback –

<jsp:fallback  ></jsp:fallback>

12- plugin – generates browser-specific code

<jsp:plugin code=”c1″ codebase=”sert” type=”bean” ></jsp:plugin>

13- output – under development to manipulate output files info

<jsp:output doctype-public=”yes” doctype-root-element=”rootel” doctype-system=”mast” omit-xml-declaration=”no” ></jsp:output>