---09 12 07---
编写一个Servlet,实际上就是编写一个实现javax.servlet.Servlet接口的类,除此之外没什么特别的。
一个简单的Servlet:
用户从浏览器输入用户名然后提交到FirstServlet,Servlet接受请求并响应用户向前台输出用户所输入的用户名
package com.cric.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class for Servlet: FirstServlet
*
*/
public class FirstServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
static final long serialVersionUID = 1L;
/* (non-Java-doc)
* @see javax.servlet.http.HttpServlet#HttpServlet()
*/
public FirstServlet() {
super();
}
/* (non-Java-doc)
* @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,
IOException {
// TODO Auto-generated method stub
String userName=request.getParameter("userName");
response.getWriter().write("do get:"+userName);
}
/* (non-Java-doc)
* @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
IOException {
// TODO Auto-generated method stub
String userName=request.getParameter("userName");
response.getWriter().write("do post:"+userName);
}
}
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
get
<form method="get" action="./FirstServlet">
User Name:<input name="userName" type="text"/>
<input type="submit" value="submit">
</form>
post
<form method="post" action="./FirstServlet">
User Name:<input name="userName" type="text"/>
<input type="submit" value="submit">
</form>
</body>
</html>
---这里要注意Servlet和html页面的字符编码方式,有时会有乱码。
---在这里先不说Servlet的配置,我接触Servlet是从ide工具开始的,怕说不好,所以...还是先不说吧。
以后我会认真学习,补充自己的不足,研究问题刨根问底。