web.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" 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">
<display-name>Test</display-name>
<description>编写一个测试的servlet类</description>
    <servlet>
          <servlet-name>testServlet</servlet-name> <!-- 首先,初始化参数、定制的URL模式以及其他定制通过此注册名而不是类名引用此servlet 其次,可在 URL而不是类名中使用此名称。因此,利用刚才给出的定义 -->
          <servlet-class>com.JR.TestServlet</servlet-class>
          <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
          <servlet-name>testServlet</servlet-name>
          <url-pattern>/test</url-pattern>
 </servlet-mapping>

 <welcome-file-list >
  <welcome-file>jsp/com/JR/login.jsp</welcome-file>
 </welcome-file-list>
</web-app>

Java文件代码:

package com.JR;

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.PropertyConfigurator;

/**
 * Servlet implementation class TestServlet
 * ctrl + shift + F 搜狗繁简转换
 * @function
 * @JRockLi
 * @2014年9月18日 下午7:32:11
 * @lilf@inspur.com Inspur
 * @Company Inspur
 */
@WebServlet("/TestServlet")
public class TestServlet extends HttpServlet {
 private static final long serialVersionUID = 1L;
 
 private Log log=LogFactory.getLog(TestServlet.class);
 
    public TestServlet() {
        super();
    }
   
 public void init(ServletConfig config) throws ServletException {
  
 }
 
 public void destroy() {
  
 }
 
 public ServletConfig getServletConfig() {
  return null;
 }
 
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  String pathDir = System.getProperty("user.dir");
  String pathDirReplaceNext = pathDir.replace("\\", "/");
  String strEnd = "/$war/WEB-INF/classes/log4j.properties";
  String relativePath = pathDirReplaceNext + strEnd;// 相对路径
  PropertyConfigurator.configure(relativePath);
  try {
   request.setCharacterEncoding("UTF-8");
   String userName=request.getParameter("userName");
   String passWord=request.getParameter("passWord");
   if(log.isDebugEnabled()){
    log.debug("JRockLi====从界面获得参数==="+userName+"==密码="+passWord);
   }
   if("lilf".equals(userName)&& "123".equals(passWord)){
    response.sendRedirect("jsp/com/JR/success.jsp");//前面省略的地址:http://localhost:8080/Test/
   }else{
    request.getRequestDispatcher("jsp/com/JR/error.jsp").forward(request, response);//前面省略的地址:http://localhost:8080/Test/
   }
  } catch (Exception e) {
   if(log.isErrorEnabled()){
    log.error("JRockLi===跳转返回页面不成功==",e);
   }
  }
  
 }

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

}

jsp文件:

<!DOCTYPE HTML>
<%@ page language="java" contentType="text/html; charset=utf-8"   pageEncoding="utf-8"%><!-- 没有这句代码,界面会是乱码 定义整个页面的编码方式-->
<html>
<head>
<meta charset="UTF-8" content="text/html" http-equiv="content-type"/>
<title>SUCCESS PAGE</title>
</head>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/skin";
%>
<script type="text/javascript" src="<%=basePath %>/js/jquery-1.11.1.js"></script>
<body>
<p>LOGIN</p>
<form >
<table>
<tr>
<td>用户名:</td>
<td><input id="userName" name="userName" value="lilf"/></td>
</tr>
<tr>
<td>密码:</td>
<td><input id="passWord" name="passWord" value="123"/></td>
</tr>
<tr><td><input type="button" id="buttonSubmit" name="buttonSubmit" value="提交" onclick="submitMethod()"></td></tr>
</table>
</form>
</body>
<script type="text/javascript">
function submitMethod(){
 var userName=$("#userName").val();
 var passWord=$("#passWord").val();
 document.forms[0].action="http://localhost:8080/Test/test";
 document.forms[0].submit();
}
</script>
</html>


备注:写的一个测试servlet的类,前台页面用到的是HTML5,用了加载log4j.properties文件,来打印自己追踪错误信息。