标签:
1.servelet
service
method package cn.dragon.servlet; //下面是导入相应的包 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; /** * 这是第一个Servlet的例子 * @author cn.dragon */ public class ServletDemoFirst extends HttpServlet { //用于处理客户端发送的GET请求 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=GB2312"); //这条语句指明了向客户端发送的内容格式和采用的字符编码. PrintWriter out = response.getWriter(); out.println(" 您好!"); //利用PrintWriter对象的方法将数据发送给客户端 out.close(); } //用于处理客户端发送的POST请求 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); //这条语句的作用是,当客户端发送POST请求时,调用doGet()方法进行处理 } }
以下截取部分 <servlet> <description>任意</description> <display-name>任意</display-name> <servlet-name>ServletDemoFirst</servlet-name> <servlet-class>cn.dragon.servlet.ServletDemoFirst</servlet-class> </servlet> <servlet-mapping> <servlet-name>ServletDemoFirst</servlet-name> <url-pattern>/servlet/ServletDemoFirst</url-pattern> </servlet-mapping> 【注意】 ① 上面的两个<servlet-name>必须相同 ② <servlet-class>后面指在对应的类上面. 技巧:你可以直接在你的servlet类中复制过来,这样可以避免出错! ③ <url-pattern> 必须是/servlet 再加servlet名字.大家现在就这么记.
package cn.dragon.servlet; 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; public class ServletDemoSecond extends HttpServlet { //初始化 public void init() throws ServletException { System.out.println("我是init()方法!用来进行初始化工作"); } //处理GET请求 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("我是doGet()方法!用来处理GET请求"); response.setContentType("text/html;charset=GB2312"); PrintWriter out = response.getWriter(); out.println("<HTML>"); out.println("<BODY>"); out.println("这是Servlet的例子"); out.println("</BODY>"); out.println("</HTML>"); } //处理POST请求 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } //销毁实例 public void destroy() { super.destroy(); System.out.println("我是destroy()方法!用来进行销毁实例的工作"); } } web.xml文件 <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <servlet> <servlet-name>ServletDemoSecond</servlet-name> <servlet-class>cn.dragon.servlet.ServletDemoSecond</servlet-class> </servlet> <servlet-mapping> <servlet-name>ServletDemoSecond</servlet-name> <url-pattern>/servlet/ServletDemoSecond</url-pattern> </servlet-mapping> </web-app>
2.javabean
JavaBean是符合某种规范的Java组件,也就是Java类。
它必须满足如下规范:
1)必须有一个零参数的默认构造函数
2)必须有get和set方法,类的字段必须通过get和set
方法来访问。
(get方法无参,set方法有参)
我们下面就来看一个JavaBean的例子。
一般网页登陆都需要用户名和密码,我们可以把用户名和密码存放在JavaBean中。
代码如下:
package mytrain.formbean; public class userBean { private String name; private String password; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
访问方法有2种: 直接访问和JSP标签访问
(不过我们推荐通过标签来访问)
1)直接访问
a) 声明bean
在页面顶部导入JavaBean类: <%@ page import="mytrain.formbean.userBean" %>
在JSP段实例化类: <% userBean user = new userBean(); %>
b) 访问bean
<% user.setXXX(aa); %>
<%=user.getXXX();%>
2)标签访问 (推荐)
a) 声明bean
使用userBean标签:
<jsp:useBean id="user" class="mytrain.formbean.userBean"/>
b) 访问bean
通过setProperty标签来设置:
<jsp:setProperty name="user" property="name" param="mUserName"/>
通过getProperty标签来取得:
<jsp:getProperty name="user" property="name"/>
下面我们来看一下完整的代码。
标签访问法: <%@ page language="java" pageEncoding="GBK"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>表单Bean例子2</title> <link rel="StyleSheet" href="../../CSS/style.css" type="text/css" /> </head> <body> <form action="" method="post"> <table> <tr> <td> <span class="blue10">用户名:</span> </td> <td> <input type="text" name="mUserName" size="20"><br> </td> </tr> <tr> <td> <span class="blue10">密 码:</span> </td> <td> <input type="password" name="mPassword" size="20"><br> </td> </tr> <tr> <td> </td> <td> <input type=submit value="submit"/> </td> </tr> </table> </form> <jsp:useBean id="user" class="mytrain.formbean.userBean"/> <jsp:setProperty name="user" property="name" param="mUserName"/> <jsp:setProperty name="user" property="password" param="mPassword"/> <hr/> 用户名:<jsp:getProperty name="user" property="name"/> <br> 密 码:<jsp:getProperty name="user" property="password"/> </body> </html>
直接访问法: <%@ page language="java" pageEncoding="GBK"%> <%@ page import="mytrain.formbean.userBean" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>表单Bean例子3</title> <link rel="StyleSheet" href="../../CSS/style.css" type="text/css" /> </head> <body> <form action="" method="post"> <table> <tr> <td> <span class="blue10">用户名:</span> </td> <td> <input type="text" name="mUserName" size="20"><br> </td> </tr> <tr> <td> <span class="blue10">密 码:</span> </td> <td> <input type="password" name="mPassword" size="20"><br> </td> </tr> <tr> <td> </td> <td> <input type=submit value="submit"/> </td> </tr> </table> </form> <% userBean user = new userBean(); user.setName(request.getParameter("mUserName")); user.setPassword(request.getParameter("mPassword")); %> <hr/> 用户名:<%=user.getName()%> <br> 密 码:<%=user.getPassword()%> </body> </html>
标签:
原文地址:http://www.cnblogs.com/tswcypy/p/4484725.html