Servlet Examples
Servlet Examples
Servlet Examples
1.Hello World
output:
code:
1.import java.io.*;
2.import javax.servlet.*;
3.import javax.servlet.http.*;
4.
5.public class HelloWorld extends HttpServlet {
6.
7. public void doGet(HttpServletRequest request, HttpServletResponse response)
8. throws IOException, ServletException
9. {
10. response.setContentType("text/html");
11. PrintWriter out = response.getWriter();
12. out.println("<html>");
13. out.println("<head>");
14. out.println("<title>Hello World!</title>");
15. out.println("</head>");
16. out.println("<body>");
17. out.println("<h1>Hello World!</h1>");
18. out.println("</body>");
19. out.println("</html>");
20. }
21.}
2.Request Info
output:
code:
1.import java.io.*;
2.import javax.servlet.*;
3.import javax.servlet.http.*;
4.
5.public class RequestInfo extends HttpServlet {
6.
7. public void doGet(HttpServletRequest request, HttpServletResponse response)
8. throws IOException, ServletException
9. {
10. response.setContentType("text/html");
11. PrintWriter out = response.getWriter();
12. out.println("<html>");
13. out.println("<body>");
14. out.println("<head>");
15. out.println("<title>Request Information Example</title>");
16. out.println("</head>");
17. out.println("<body>");
18. out.println("<h3>Request Information Example</h3>");
19. out.println("Method: " + request.getMethod());
20. out.println("Request URI: " + request.getRequestURI());
21. out.println("Protocol: " + request.getProtocol());
22. out.println("PathInfo: " + request.getPathInfo());
23. out.println("Remote Address: " + request.getRemoteAddr());
24. out.println("</body>");
25. out.println("</html>");
26. }
27.
28. /**
29. * We are going to perform the same operations for POST requests
30. * as for GET methods, so this method just sends the request to
31. * the doGet method.
32. */
33.
34. public void doPost(HttpServletRequest request, HttpServletResponse response)
35. throws IOException, ServletException
36. {
37. doGet(request, response);
38. }
39.}
3.Request Headers
output:
code:
1.import java.io.*;
2.import java.util.*;
3.import javax.servlet.*;
4.import javax.servlet.http.*;
5.
6.public class RequestHeaderExample extends HttpServlet {
7.
8. public void doGet(HttpServletRequest request, HttpServletResponse response)
9. throws IOException, ServletException
10. {
11. response.setContentType("text/html");
12. PrintWriter out = response.getWriter();
13. Enumeration e = request.getHeaderNames();
14. while (e.hasMoreElements()) {
15. String name = (String)e.nextElement();
16. String value = request.getHeader(name);
17. out.println(name + " = " + value);
18. }
19. }
20.}
4. Request Parameters
output:
code:
1.import java.io.*;
2.import java.util.*;
3.import javax.servlet.*;
4.import javax.servlet.http.*;
5.
6.public class RequestParamExample extends HttpServlet {
7.
8. public void doGet(HttpServletRequest request, HttpServletResponse response)
9. throws IOException, ServletException
10. {
11. response.setContentType("text/html");
12. PrintWriter out = response.getWriter();
13. out.println("<html>");
14. out.println("<head>");
15. out.println("<title>Request Parameters Example</title>");
16. out.println("</head>");
17. out.println("<body>");
18. out.println("<h3>Request Parameters Example</h3>");
19. out.println("Parameters in this request:<br>");
20. if (firstName != null || lastName != null) {
21. out.println("First Name:");
22. out.println(" = " + HTMLFilter.filter(firstName) + "<br>");
23. out.println("Last Name:");
24. out.println(" = " + HTMLFilter.filter(lastName));
25. } else {
26. out.println("No Parameters, Please enter some");
27. }
28. out.println("<P>");
29. out.print("<form action=\"");
30. out.print("RequestParamExample\" ");
31. out.println("method=POST>");
32. out.println("First Name:");
33. out.println("<input type=text size=20 name=firstname>");
34. out.println("<br>");
35. out.println("Last Name:");
36. out.println("<input type=text size=20 name=lastname>");
37. out.println("<br>");
38. out.println("<input type=submit>");
39. out.println("</form>");
40. out.println("</body>");
41. out.println("</html>");
42. }
43.
44. public void doPost(HttpServletRequest request, HttpServletResponse res)
45. throws IOException, ServletException
46. {
47. doGet(request, response);
48. }
49.}
5. Cookies
output:
code:
1.import java.io.*;
2.import javax.servlet.*;
3.import javax.servlet.http.*;
4.
5.public class CookieExample extends HttpServlet {
6.
7. public void doGet(HttpServletRequest request, HttpServletResponse response)
8. throws IOException, ServletException
9. {
10. response.setContentType("text/html");
11. PrintWriter out = response.getWriter();
12.
13. // print out cookies
14.
15. Cookie[] cookies = request.getCookies();
16. for (int i = 0; i < cookies.length; i++) {
17. Cookie c = cookies[i];
18. String name = c.getName();
19. String value = c.getValue();
20. out.println(name + " = " + value);
21. }
22.
23. // set a cookie
24.
25. String name = request.getParameter("cookieName");
26. if (name != null && name.length() > 0) {
27. String value = request.getParameter("cookieValue");
28. Cookie c = new Cookie(name, value);
29. response.addCookie(c);
30. }
31. }
32.}
6. Sessions
output:
code:
1.import java.io.*;
2.import java.util.*;
3.import javax.servlet.*;
4.import javax.servlet.http.*;
5.
6.public class SessionExample extends HttpServlet {
7.
8. public void doGet(HttpServletRequest request, HttpServletResponse response)
9. throws IOException, ServletException
10. {
11. response.setContentType("text/html");
12. PrintWriter out = response.getWriter();
13.
14. HttpSession session = request.getSession(true);
15.
16. // print session info
17.
18. Date created = new Date(session.getCreationTime());
19. Date accessed = new Date(session.getLastAccessedTime());
20. out.println("ID " + session.getId());
21. out.println("Created: " + created);
22. out.println("Last Accessed: " + accessed);
23.
24. // set session info if needed
25.
26. String dataName = request.getParameter("dataName");
27. if (dataName != null && dataName.length() > 0) {
28. String dataValue = request.getParameter("dataValue");
29. session.setAttribute(dataName, dataValue);
30. }
31.
32. // print session contents
33.
34. Enumeration e = session.getAttributeNames();
35. while (e.hasMoreElements()) {
36. String name = (String)e.nextElement();
37. String value = session.getAttribute(name).toString();
38. out.println(name + " = " + value);
39. }
40. }
41.}
Servlet Examples的更多相关文章
- 漫谈servlet技术
1.要谈到Servlet技术,不得不先谈谈动态网页的概念. 编写过网页的人都知道,浏览器能够根据HTML静态标记语言来显示各式各样的网页.但是如果我们需要在网页上完成一些业务逻辑:比如登陆验证.或者说 ...
- 115 Java Interview Questions and Answers – The ULTIMATE List--reference
In this tutorial we will discuss about different types of questions that can be used in a Java inter ...
- 如何配置Tomcat以使用Apache httpd?
How to Connect Tomcat 6 to Apache HTTP Server 2 Tomcat can be run as a standalone server. Tomcat can ...
- Java Code Examples for javax.servlet.http.Part
http://www.programcreek.com/java-api-examples/index.php?api=javax.servlet.http.Part The following ar ...
- Tomcat7配置及其servlet调用详解
Tomcat 1 Tomcat简介 Tomcat是一个免费的开源的Serlvet容器,它是Apache基金会的Jakarta项目中的一个核心项目,由Apache,Sun和其它一些公司及个人共同开发而成 ...
- Java面试题总结系列 Servlet
Servlet技术主要是为了使用Web上的HTTP协议而设计的.servlet是在WEB服务器上运行的程序.Java Servlet可以用于处理客户请求或生成动态Web网页.先一个实例.然后解释. 先 ...
- Servlet 工作原理解析
转自:http://www.ibm.com/developerworks/cn/java/j-lo-servlet/ Web 技术成为当今主流的互联网 Web 应用技术之一,而 Servlet 是 J ...
- JavaWeb总结--Servlet 工作原理解析
从 Servlet 容器说起 要介绍 Servlet 必须要先把 Servlet 容器说清楚,Servlet 与 Servlet 容器的关系有点像枪和子弹的关系,枪是为子弹而生,而子弹又让枪有了杀伤力 ...
- 2 Servlet基础
作者:禅楼望月(http://www.cnblogs.com/yaoyinglong) 1. 从浏览器访问Servlet的流程 刚才发现,这里的图片不能正常显示,所以我给个链接,大家可以下载下来看从浏 ...
随机推荐
- VLD 1.0 ReadMe翻译尝试
近期想学习下VLD的实现,打算从最简单的V1.0版本看起.以下是V1.0版本自己尝试翻译下,最新的2.x版本似乎强大了很多. 简介 Visual C++提供了内置的内存检测机制,但其充其量只满足了最小 ...
- java基础知识2
58.线程的基本概念.线程的基本状态以及状态之间的关系线程指在程序执行过程中,能够执行程序代码的一个执行单位,每个程序至少都有一个线程,也就是程序本身.Java中的线程有四种状态分别是:运行.就绪.挂 ...
- (转) Special members
原地址:http://www.cplusplus.com/doc/tutorial/classes2/ Special members [NOTE: This chapter requires p ...
- php 大流量网站访问
1:确认服务器硬件能否支持当前流量 2:数据库优化,用到什么字段查什么字段,减轻查询负担. 3:静态化,缓存,减少连库操作. 4:禁止外部盗链,减轻负载压力. 5:控制文件下载大小,尽量不超过2M,有 ...
- 自定义view组件
参考<疯狂android讲义>第2版 2.1节P48,对应CustomViewDemo.zip. 若在开发过程中,发现现有的view均不能满足需要,可以自定义一个view. 自定义一个vi ...
- Ubuntu14.0.4 64位安装Chrome浏览器
下载链接:translate.google.com.hk/translate?hl=zh-CN&sl=en&u=http://95.31.35.30/chrome/pool/main/ ...
- Codeforces 494B Obsessive String
http://www.codeforces.com/problemset/problem/494/B 题意:给出两个串S,T,求有几种将S分成若干个子串,满足T都是这若干个子串的子串. 思路:f[n] ...
- eMMC的MMC模式与SPI模式
MMC存贮卡可以分为MMC和SPI两种工作模式,MMC模式是标准的默认模式,具有MMC的全部特性.而SPI模式则是MMC存贮卡可选的第二种模式,这个模式是MMC协议的一个子集,主要用于只需要小数量的卡 ...
- Qt之HTTPS登录(集成QNetworkAccessManager提前修改QSslConfiguration,然后post)
简述 HTTPS(全称:Hyper Text Transfer Protocol over Secure Socket Layer),是以安全为目标的HTTP通道,简单讲是HTTP的安全版.即HTTP ...
- easy_install django==1.4.2_百度搜索
easy_install django==1.4.2_百度搜索 安装指定版本的django