Servlet简明教程

转自:http://www.blogjava.net/jlin/articles/62523.html

  • Servlet程序的基本结构

//Servlet  基本结构

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class SomeServlet extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// 用 "request" 可以读取输入的值 (e.g. cookies)
// 和表单提交的数据
// 用 "response" 返回输出的内容

PrintWriter out = response.getWriter();

// 用 "out" 向浏览器写内容。

}
}

Servlet都是有HttpServlet继承下来的。基本结构如上面的代码所示。

  • Servlet实例

1、生成一般文本的例子


package test;
//用这条语句编译 javac -encoding iso8859_1 filename.java

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class HelloServlet extends HttpServlet {
public void doGet( HttpServletRequest requset,
HttpServletResponse response)
throws IOException, ServletException {
PrintWriter out = response.getWriter();
out.println("Hello,world!");
out.println("你好,万维网");
}
}

2、生成HTML内容的例子

package test;
//javac -encoding iso8859_1 HelloWWW.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class HelloWWW extends HttpServlet {
public void doGet( HttpServletRequest requset,
HttpServletResponse response)
throws IOException, ServletException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">"+
"<HTML>"+
"<HEAD>"+
"<TITLE>"+"Hello WWW by LoveJSP.site"+"</TITLE>"+
"<BODY>"+
"<H1>Hello WWW</H1>"+
"<H1>你好,万维网</H1>"+
"<h2><A HREF=\"HTTP://LOVEJSP.COM\">welcom to LoveJSP.site</A></h2>"+
"</BODY>"+
"<HTML>");
}
}

  • 处理表单提交的数据

在Web程序设计中,处理表单提交的数据是获取Web数据的主要方法,今天,我们来看一看Servlet中是怎样处理来自表单的数据的。
表单数据的提交方法有两种:Post方法和Get方法。当使用Post方法时,数据由标准的输入设备读入,当使用Get方法时,数据由CGI变量QUERY_STRING传递给表单数据处理程序。Servlet会自动将以上两种方法得到的数据进行处理,从而使用户只要简单的调用HttpServletRequest的getParameter方法,给出变量名称即可取得该变量的值。需要注意的是,变量的名称是大小写敏感的。对于Post方法或Get方法提交的数据,Servlet的处理方法是一样的。当请求的变量不存在时,将会返回一个空字符串。如果变量有多个值,你应该调用getParameterValues,这个方法将会返回一个字符串数组。使用getParameterNames可以取得所有变量的名称,该方法返回一个Emumeration方法。
下面让我们来看一个简单的例子,下面这个Servlet读取表单中指定名称的五个字段的值。下载这个例子

1.页面文件

postdata.html

<html>
<head>
<title>getFormData Servlet Example form LoveJSP.com</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>

<body bgcolor="#FFFFFF">
<h1 align="center"> <i><b>Demo Page</b></i></h1>
<form action="/try/servlet/lovejsp.getFormData">
<p> </p>
<table width="41%" border="2" align="center">
<tr bgcolor="#6633CC" align="center">
<td colspan="2" align="center"><font color='white'>getFormData Servlet DemoPage</font></td>
</tr>
<tr bgcolor="#FFFFCC">
<td align="center" width="43%">
<div align="right">username:</div>
</td>
<td width="57%">
<div align="left">
<input type="text" name="username">
</div>
</td>
</tr>
<tr bgcolor="#CCFF99">
<td align="center" width="43%">
<div align="right">password:</div>
</td>
<td width="57%">
<div align="left">
<input type="password" name="password">
</div>
</td>
</tr>
<tr bgcolor="#FFFFCC">
<td align="center" width="43%">
<div align="right">Email:</div>
</td>
<td width="57%">
<div align="left">
<input type="text" name="email">
</div>
</td>
</tr>
<tr bgcolor="#CCFF99">
<td align="center" width="43%">
<div align="right">Homepage:</div>
</td>
<td width="57%">
<div align="left">
<input type="text" name="Homepage">
</div>
</td>
</tr>
</table>
<p align="center">
<input type="reset" name="Reset" value="clear">
<input type="submit" name="Submit2" value="Let's Go">
</p>
</form>
</body>
</html>

2.Servlet文件

getFormData.java

package lovejsp;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

/** Servlet getParameter Ex from Lovejsp.site(http://www.lovejsp.com)
*/

public class getFormData extends HttpServlet {
  public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    String title = "读取表单数据";
    out.println(LovejspTools.headTitle(title) + //a tools method to show the html code with title
    "<BODY BGCOLOR=\"#FDF5E6\">\n" +
    "<H1 ALIGN=CENTER>" + title + "</H1>\n" +
    "<UL>\n" +
    " <LI><B>username</B>: "
    + request.getParameter("username") + "\n" +   
    " <LI><B>password</B>: "
    + request.getParameter("password") + "\n" +
    " <LI><B>Email</B>: "
    + request.getParameter("Email") + "\n" +
    " <LI><B>Homepage</B>: "
    + request.getParameter("Homepage") + "\n" +
    "</UL>\n" +
    "</BODY></HTML>");
}
}

3.getParameterNames的示例

下面这个程序是getParameterNames的示例,也是调试程序的很好的工具,它的功能是显示所有的表单数据。

ShowAllFormData.java 

package lovejsp;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;

public class ShowAllFormData extends HttpServlet {
public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String title = "显示所有的Form变量的值";
out.println("<HTML><HEAD><TITLE>"+title+"</TITLE></HEAD>"+
"<BODY BGCOLOR=\"#FDF5E6\">\n" +
"<H1 ALIGN=CENTER>" + title + "</H1>\n" +
"<TABLE BORDER=1 ALIGN=CENTER>\n" +
"<TR BGCOLOR=\"#FFAD00\">\n" +
"<TH>变量名称<TH>变量值");
Enumeration paramNames = request.getParameterNames();
while(paramNames.hasMoreElements()) {
String paramName = (String)paramNames.nextElement();
out.println("<TR><TD>" + paramName + "\n<TD>");
String[] paramValues = request.getParameterValues(paramName);
if (paramValues.length == 1) {
String paramValue = paramValues[0];
if (paramValue.length() == 0)
out.print("<I>No Value</I>");
else
out.print(paramValue);
} else {
out.println("<UL>");
for(int i=0; i<paramValues.length; i++) {
out.println("<LI>" + paramValues[i]);
}
out.println("</UL>");
}
}
out.println("</TABLE>\n</BODY></HTML>");
}

public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值