studyservlet
servlet使用入门
servlet API 包含两个包中,即javax.servlet和javax,servlet.http.
javax.servlet包的主要的类和接口
javax.servlet
接口 类
ServletConfig ServletOutputStream
ServletContext ServletInputStream
ServletResponse GenericServlet
ServletRequest
Servlet
javax.servlet.http
接口 类
HttpSession Cookie
HttpSessionAttributeListener HttpServlet
HttpServletRequest
HttpServeltResponse
2实例(源文件见我的空间)
package my.servlet;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
@SuppressWarnings("serial")
public class LoginServlet extends HttpServlet {
public LoginServlet() {
try {
jbInit();
} catch (Exception ex) {
ex.printStackTrace();
}
}
private static final String CONTENT_TYPE = "text/html; charset=gb2312";
//Initialize global variables
public void init() throws ServletException {
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
response.setContentType(CONTENT_TYPE);
request.setCharacterEncoding("GBK");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>Servlet</title></head>");
out.println("<body bgcolor=/"#ffffff/">");
String UserId=request.getParameter("userid");
String PassWord=request.getParameter("password");
String UserName=request.getParameter("username");
String Sex=request.getParameter("men");
String Age=request.getParameter("age");
out.println("<table border=0 align=center width=50%>");
out.println("<tr><td colspan=2><center><font color=red size=6>用户注册详细信息</font></center></td></tr>");
out.println("<tr><td>用户 ID:</td><td><font color=red face=楷体>"+UserId+"</font></td></tr>");
out.println("<tr><td>密 码:</td><td><font color=red face=宋体>"+PassWord+"</font></td></tr>");
out.println("<tr><td>姓 名:</td><td><font color=red face=楷体>"+UserName+"</font></td></tr>");
out.println("<tr><td>性 别:</td><td><font color=red face=宋体>"+Sex+"</font></td></tr>");
out.println("<tr><td>年 龄:</td><td><font color=red face=宋体>"+Age+"</font></td></tr>");
out.println("</body>");
out.println("</html>");
out.close();
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
doGet(request, response);
}
//Clean up resources
public void destroy() {
}
private void jbInit() throws Exception {
}
}
web.xml
添加如下两句话
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>my.servlet.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/loginservlet</url-pattern>
</servlet-mapping>
使用
index.jsp
<%@ page language="java" import="java.util.*" contentType="text/html; charset=GB2312"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body bgcolor="#ffffff">
<form action="/loginservlet" method="get"> //由于LoginServlet只定义了get方法,这里的method写作get,如果写作post,则取不值
<table border=0 align=center width=50%>
<tr><td colspan=2><center><font color=red size=6>用户注册详细信息</font></center></td></tr>
<tr><td>用户 ID:</td><td><input type="text" name="userid"> </td></tr>
<tr><td>密 码:</td><td><input type="password" name="password"></td></tr>
<tr><td>姓 名:</td><td><input type="text" name="username"></td></tr>
<tr><td>性 别:</td><td><input type="radio" name="men" value="男" checked="checked">男 <input type="radio" name="men" value="女">女</tr>
<tr><td>年 龄:</td><td><input type="text" name="age"></td></tr>
<tr><td colspan="2"> <input type="submit" value="注册"></td></tr>
</table>
</form>
</body>
</html>
作业:
1编写一个线程安全的Servlet以便显示该Servlet被访问的次数.
package my.servlet;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
@SuppressWarnings("serial")
public class LoginServlet extends HttpServlet {
int count;
private static final String CONTENT_TYPE = "text/html; charset=gb2312";
public LoginServlet() {
try {
jbInit();
} catch (Exception ex) {
ex.printStackTrace();
}
}
//Initialize global variables
public void init(ServletConfig config) throws ServletException {
super.init(config);
String initial=config.getInitParameter("initial");
try
{
count=Integer.parseInt(initial);
}
catch(NumberFormatException e)
{count=0;}
System.out.println("计数器servlet已经初始化了");
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
count++;
System.out.println("该servlet的doGet方法被执行了"+count+"次");
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
doPost(request, response);
}
//Clean up resources
public void destroy() {
System.out.println("计数器servlet已经释放资源");
}
private void jbInit() throws Exception {
}
}
2.编写一个Servlet显示客户的区域信息(包括语言和国家)