<%-- 网站: <a href="http://www.crazyit.org">疯狂Java联盟</a> author yeeku.H.lee kongyeeku@163.com version 1.0 Copyright (C), 2001-2016, yeeku.H.Lee This program is protected by copyright laws. Program Name: Date: --%> <%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title> 读取Cookie </title> <meta name="website" content="http://www.crazyit.org" /> </head> <body> <% // 获取本站在客户端上保留的所有Cookie Cookie[] cookies = request.getCookies(); // 遍历客户端上的每个Cookie for (Cookie c : cookies) { // 如果Cookie的名为username,表明该Cookie是需要访问的Cookie if(c.getName().equals("username")) { out.println(c.getValue()); } } %> </body> </html>
<%-- 网站: <a href="http://www.crazyit.org">疯狂Java联盟</a> author yeeku.H.lee kongyeeku@163.com version 1.0 Copyright (C), 2001-2016, yeeku.H.Lee This program is protected by copyright laws. Program Name: Date: --%> <%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title> redirect目标页 </title> <meta name="website" content="http://www.crazyit.org" /> </head> <body> <h3>被重定向的目标页</h3> name请求参数的值:<%=request.getParameter("name")%> </body> </html>
<%-- 网站: <a href="http://www.crazyit.org">疯狂Java联盟</a> author yeeku.H.lee kongyeeku@163.com version 1.0 Copyright (C), 2001-2016, yeeku.H.Lee This program is protected by copyright laws. Program Name: Date: --%> <%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %> <%@ page import="java.util.*" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title> 获取请求头/请求参数 </title> <meta name="website" content="http://www.crazyit.org" /> </head> <body> <% // 获取所有请求头的名称 Enumeration<String> headerNames = request.getHeaderNames(); while(headerNames.hasMoreElements()) { String headerName = headerNames.nextElement(); // 获取每个请求、及其对应的值 out.println( headerName + "-->" + request.getHeader(headerName) + "<br/>"); } out.println("<hr/>"); // 设置解码方式,对于简体中文,使用GBK解码 request.setCharacterEncoding("GBK"); // ① // 下面依次获取表单域的值 String name = request.getParameter("name"); String gender = request.getParameter("gender"); // 如果某个请求参数有多个值,将使用该方法获取多个值 String[] color = request.getParameterValues("color"); String national = request.getParameter("country"); %> <!-- 下面依次输出表单域的值 --> 您的名字:<%=name%><hr/> 您的性别:<%=gender%><hr/> <!-- 输出复选框获取的数组值 --> 您喜欢的颜色:<%for(String c : color) {out.println(c + " ");}%><hr/> 您来自的国家:<%=national%><hr/> </body> </html>
<%-- 网站: <a href="http://www.crazyit.org">疯狂Java联盟</a> author yeeku.H.lee kongyeeku@163.com version 1.0 Copyright (C), 2001-2016, yeeku.H.Lee This program is protected by copyright laws. Program Name: Date: --%> <%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title> 获取GET请求参数 </title> <meta name="website" content="http://www.crazyit.org" /> </head> <body> <% // 获取name请求参数的值 String name = request.getParameter("name"); // 获取gender请求参数的值 String gender = request.getParameter("gender"); %> <!-- 输出name变量值 --> 您的名字:<%=name%><hr/> <!-- 输出gender变量值 --> 您的性别:<%=gender%><hr/> </body> </html>
<%-- 网站: <a href="http://www.crazyit.org">疯狂Java联盟</a> author yeeku.H.lee kongyeeku@163.com version 1.0 Copyright (C), 2001-2016, yeeku.H.Lee This program is protected by copyright laws. Program Name: Date: --%> <%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title> 获取包含非西欧字符的GET请求参数 </title> <meta name="website" content="http://www.crazyit.org" /> </head> <body> <% // 获取请求里包含的查询字符串 String rawQueryStr = request.getQueryString(); out.println("原始查询字符串为:" + rawQueryStr + "<hr/>"); // 使用URLDecoder解码字符串 String queryStr = java.net.URLDecoder.decode( rawQueryStr , "UTF-8"); out.println("解码后的查询字符串为:" + queryStr + "<hr/>"); // 以&符号分解查询字符串 String[] paramPairs = queryStr.split("&"); for(String paramPair : paramPairs) { out.println("每个请求参数名、值对为:" + paramPair + "<br/>"); // 以=来分解请求参数名和值 String[] nameValue = paramPair.split("="); out.println(nameValue[0] + "参数的值是:" + nameValue[1]+ "<hr/>"); } %> </body> </html>
<%-- 网站: <a href="http://www.crazyit.org">疯狂Java联盟</a> author yeeku.H.lee kongyeeku@163.com version 1.0 Copyright (C), 2001-2016, yeeku.H.Lee This program is protected by copyright laws. Program Name: Date: --%> <%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %> <%@ page import="java.util.*" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title> request处理 </title> <meta name="website" content="http://www.crazyit.org" /> </head> <body> <% // 取出请求参数 String bal = request.getParameter("balance"); double qian = Double.parseDouble(bal); // 取出request范围内的info属性 List<String> info = (List<String>)request.getAttribute("info"); for (String tmp : info) { out.println(tmp + "<br/>"); } out.println("取钱" + qian + "块"); out.println("账户减少" + qian); %> </body> </html>
<%-- 网站: <a href="http://www.crazyit.org">疯狂Java联盟</a> author yeeku.H.lee kongyeeku@163.com version 1.0 Copyright (C), 2001-2016, yeeku.H.Lee This program is protected by copyright laws. Program Name: Date: --%> <%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %> <!DOCtype html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title> 选择物品购买 </title> <meta name="website" content="http://www.crazyit.org" /> </head> <body> <form method="post" action="processBuy.jsp"> 书籍:<input type="checkbox" name="item" value="book"/><br/> 电脑:<input type="checkbox" name="item" value="computer"/><br/> 汽车:<input type="checkbox" name="item" value="car"/><br/> <input type="submit" value="购买"/> </form> </body> </html>
<%-- 网站: <a href="http://www.crazyit.org">疯狂Java联盟</a> author yeeku.H.lee kongyeeku@163.com version 1.0 Copyright (C), 2001-2016, yeeku.H.Lee This program is protected by copyright laws. Program Name: Date: --%> <!-- 通过errorPage属性指定异常处理页面 --> <%@ page contentType="text/html; charset=GBK" language="java" errorPage="error.jsp" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title> JSP脚本的异常机制 </title> <meta name="website" content="http://www.crazyit.org" /> </head> <body> <% int a = 6; int c = a / 0; %> </body> </html>
<%-- 网站: <a href="http://www.crazyit.org">疯狂Java联盟</a> author yeeku.H.lee kongyeeku@163.com version 1.0 Copyright (C), 2001-2016, yeeku.H.Lee This program is protected by copyright laws. Program Name: Date: --%> <%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %> <% // 生成页面响应 out.println("===="); // 重定向到redirect-result.jsp页面 response.sendRedirect("redirect-result.jsp"); %>
<?xml version="1.0" encoding="GBK"?> <!-- 定义生成文件的project根元素,默认的target为空 --> <project name="web" basedir="." default=""> <!-- 定义三个简单属性 --> <property name="src" value="src"/> <property name="classes" value="classes"/> <!-- 定义一组文件和目录集 --> <path id="classpath"> <fileset dir="lib"> <include name="*.jar"/> </fileset> <pathelement path="${classes}"/> </path> <!-- 定义compile target,用于编译Java源文件 --> <target name="compile" description="编译Java源文件"> <!-- 先删除classes属性所代表的文件夹 --> <delete dir="${classes}"/> <!-- 创建classes属性所代表的文件夹 --> <mkdir dir="${classes}"/> <!-- 编译Java文件,编译后的class文件放到classes属性所代表的文件夹内 --> <javac destdir="${classes}" debug="true" includeantruntime="yes" deprecation="false" optimize="false" failonerror="true"> <!-- 指定需要编译的Java文件所在的位置 --> <src path="${src}"/> <!-- 指定编译Java文件所需要第三方类库所在的位置 --> <classpath refid="classpath"/> </javac> </target> </project>
<?xml version="1.0" encoding="GBK"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <!-- 配置第一个参数:driver --> <context-param> <param-name>driver</param-name> <param-value>com.mysql.jdbc.Driver</param-value> </context-param> <!-- 配置第二个参数:url --> <context-param> <param-name>url</param-name> <param-value>jdbc:mysql://localhost:3306/javaee</param-value> </context-param> <!-- 配置第三个参数:user --> <context-param> <param-name>user</param-name> <param-value>root</param-value> </context-param> <!-- 配置第四个参数:pass --> <context-param> <param-name>pass</param-name> <param-value>32147</param-value> </context-param> <servlet> <!-- 指定Servlet名字 --> <servlet-name>config</servlet-name> <!-- 指定将哪个JSP页面配置成Servlet --> <jsp-file>/configTest2.jsp</jsp-file> <!-- 配置名为name的参数,值为crazyit.org --> <init-param> <param-name>name</param-name> <param-value>crazyit.org</param-value> </init-param> <!-- 配置名为age的参数,值为30 --> <init-param> <param-name>age</param-name> <param-value>30</param-value> </init-param> </servlet> <servlet-mapping> <!-- 指定将config Servlet配置到/config URL--> <servlet-name>config</servlet-name> <url-pattern>/config</url-pattern> </servlet-mapping> </web-app>
package lee; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.annotation.*; import java.io.*; /** * Description: * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a> * <br/>Copyright (C), 2001-2016, Yeeku.H.Lee * <br/>This program is protected by copyright laws. * <br/>Program Name: * <br/>Date: * @author Yeeku.H.Lee kongyeeku@163.com * @version 1.0 */ @WebServlet(name="get-application", urlPatterns={"/get-application"}) public class GetApplication extends HttpServlet { public void service(HttpServletRequest request, HttpServletResponse response)throws IOException { response.setContentType("text/html;charset=gb2312"); PrintWriter out = response.getWriter(); out.println("<html><head><title>"); out.println("测试application"); out.println("</title></head><body>"); ServletContext sc = getServletConfig().getServletContext(); out.print("application中当前的counter值为:"); out.println(sc.getAttribute("counter")); out.println("</body></html>"); } }