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的流程 刚才发现,这里的图片不能正常显示,所以我给个链接,大家可以下载下来看从浏 ...
随机推荐
- getHibernateTemplate().find方法详解
Spring中常用的hql查询方法(getHibernateTemplate()) --------------------------------- 一.find(String queryStrin ...
- 视听说加速器--AHK辅助工具
大学有视听说这门课,看起来这门课设计得非好,可是对大多数人来讲却不能按时完成.到了最后都要抓紧提速,上网找答案,辛苦的抄,有“聪明者”便找加速器来做. 我也是赶着做的人之一.抄答案太累,加速器太卡,还 ...
- Java 学习 第五篇;面向对象
1:基本数据类型的拆装: 基本变量类型 通过 new WrapperClass(primitive) 创建包装类对象: 包装类的对象 通过 WrapperInstance.XXXValue() 获取包 ...
- UESTC_贪吃蛇 CDOJ 709
相信大家都玩过贪吃蛇游戏吧. 在n×m的迷宫中,有着一条长度不超过9的贪吃蛇,它已经将所有的食物吃光了,现在的目标是移动到出口. 它走的时候不能碰到自己的身体,也不能碰到墙壁.(如果贪吃蛇的长度> ...
- nginx编译配置
1, 正向代理是一个位于内网客户端和外网原始服务器之间的服务器,为了从原始服务器取得内容,客户端向代理发送一个请求并指定目标,然后由代理服务器向 原始服务器转交请求并将获得的内容返回给客户端.正向代理 ...
- Python基础教程学习(四)类的创建与继承
类中可以有方法,类外也可以有函数,其实类就是一种封装, Python中可以自己定义一个函数,一可以把这个函数在类中封装成一个方法, 其中的属性和方法自然就从父类中继承来了, 要想获得多个类的属性和功能 ...
- 自定义View的封装
通过上一个博文的自定义View的编写后,发现代码其实挺复杂的,所有这里通过对代码进一步的优化,即用到了java的一个重要的特点:封装.通过对父类的方法进行封装,然后在子类继承后实现相应的函数即可,达到 ...
- Eclipse/IDEA中使用Maven创建Web项目报错
Eclipse中的错误:Could not resolve archetype org.apache.maven.archetypes:maven-archetype-webapp-1.0.jar:R ...
- os x 10.10 測试版系统下载 swift语言学习资料下载
http://pan.baidu.com/s/1eQ5oj1S 这是下载地址 ! 刚学完oc 就出了 swift!这---- 只是还是非常高兴看了一点swith得东西感觉 ...
- jQuery支持移动Mobile的DOM元素移动和缩放插件
jQuery Panzoom是一款很有用的HTML DOM元素平移和缩放jQuery和CSS3插件. Panzoom利用CSS transforms 和 matrix函数来为浏览器进行硬件(GPU)加 ...