Servlet 3
Servlet 3
Servlet 3
======================
We can communicate to servlet program in three ways.
A request which is generated by using hyperlink does not carry the data.
A request which is generated by using form page will carry the data.
In HTML based hyperlink to servlet communication we need to type our request url as
href url.
ex:
<a href="http://localhost:2525/DateApp/test"> click </a>
In HTML based form page to servlet communication we need to type our request url as
action url.
ex:
<form action="http://localhost:2525/DateApp/test">
-
-
-
</form>
Note:
----
It is never recommanded to extends a class with GenericServlet class because it
won't give HTTP protocol features.
index.html
----------
<center>
<h1>
<a href="test"> getMsg </a>
</h1>
</center>
web.xml
-------
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<servlet>
<servlet-name>WishSrv</servlet-name>
<servlet-class>com.ihub.www.WishSrv</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>WishSrv</servlet-name>
<url-pattern>/test</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
WishSrv.java
-----------
package com.ihub.www;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Calendar;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
Calendar c=Calendar.getInstance();
int h=c.get(Calendar.HOUR_OF_DAY);
if(h<12)
pw.println("<center><h1> Good Morning </h1></center>");
else if(h<16)
pw.println("<center><h1> Good Afternoon </h1></center>");
else if(h<20)
pw.println("<center><h1> Good Evening </h1></center>");
else
pw.println("<center><h1> Good Night </h1></center>");
pw.close();
}
}
Request url
----------
http://localhost:2525/WishApp/
1) GET methodology
----------------
It carries limited amount of data.
2) POST methodology
-------------------
It carries unlimited amount of data.
While extending a class with HttpServlet class , it is never recommanded to use
service(-,-) method because it is not designed according to HTTP protocol.
form.html
---------
</form>
web.xml
-------
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<servlet>
<servlet-name>VoteSrv</servlet-name>
<servlet-class>com.ihub.www.VoteSrv</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>VoteSrv</servlet-name>
<url-pattern>/test</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>form.html</welcome-file>
</welcome-file-list>
</web-app>
VoteSrv.java
------------
package com.ihub.www;
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;
if(age<18)
pw.println("<center><h1 style='color:red'>"+name+" U r not
eligible to vote </h1></center>");
else
pw.println("<center><h1 style='color:green'>"+name+" U r eligible
to vote </h1></center>");
pw.close();
}
}
Request url
-----------
http://localhost:2525/VoteApp/
student table
=============
drop table student;