servlet主要就是处理页面传过来的表单数据,页面上的form的action指向到web.xml中,然后在XML中对称节点中找到对应的servlet类去执行你的处理方法.
servlet中使用HttpServletRequest和HttpServletResponse来接受和返回数据
这个类要继承一个HttpServlet类然后重写他的doGet和doPost方法,一般在doGet里this.doPost(req,resp);然后具体的代码在doPost()方法中执行.
一个登陆的小例子,这个是Servlet.
package com.wangchao.action;
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;
import com.wangchao.bo.AdminBO;
import com.wangchao.service.SfInfoService;
import com.wangchao.service.SfInfoServiceImpl;
public class LoginAction extends HttpServlet{
/**
*
*/
private static final long serialVersionUID = 1L;
String name = "";
String pass = "";
SfInfoService loginser;
AdminBO bo;
boolean istrun;
PrintWriter pw;
public LoginAction(){
loginser = new SfInfoServiceImpl();
bo = new AdminBO();
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
this.doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");
resp.setContentType("text/html;charset=utf-8");
name = req.getParameter("username");
pass = req.getParameter("password");
bo.setName(name);
bo.setPassword(pass);
istrun = loginser.userLogin(bo);
pw = resp.getWriter();
if(istrun){
req.setAttribute("name", name);
req.setAttribute("pass", pass);
req.getRequestDispatcher("admin/user_info.jsp").forward(req, resp);
// resp.sendRedirect("admin/MyJsp.jsp");
}else{
pw.print("<h3>用户名或密码错误,点击<a href='admin/index.jsp'>返回</a></h3>");
pw.flush();
}
}
}
下面是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>login</servlet-name>
<servlet-class>com.wangchao.action.LoginAction</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/login.do</url-pattern>
</servlet-mapping>
</web-app>
以下为JSP页面:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
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>
<div align="center">
<form action="login.do" method="post">
用户名:<input type="text" name="username" style="color: red"><br><br>
密 码:<input type="password" name="password" style="color: green"><br><br>
<div align="center">
<input type="submit" value="登陆"> <input type="reset" value="重填">
</div>
</form>
</div>
</body>
</html>本回答被提问者采纳