JSP+Filter+Servlet的简单实例

JSP页面



<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%
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>登录页面</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>
  <form action="/See/servlet/Test1"  method="post">
  <font size="6">登录页面</font><br>
  账号:<input type="text" name="user"><br>
  密码:<input type="password" name="password"><br>
  <input type="submit" value="提交" name="button1">
  <input type="reset" value="取消" name="button2">
  </form>
  </body>
</html>




filter代码


package sey;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Filter1 implements Filter {

/**
* Destruction of the servlet. <br>
*/
public void destroy() {
// Put your code here
}


public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chains) throws IOException, ServletException {
// TODO Auto-generated method stub
//转码
request.setCharacterEncoding("GBK");
response.setContentType("text/html;charset=GBK");
//放行
chains.doFilter(request,response);
}

public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub

}

}


Servlet的代码

package sey;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Test1 extends HttpServlet {


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

/**
* 数据库的连接,设置SQL命令 Destruction of the servlet. <br>
*/
Connection conn = null;
    Statement stmt = null;

String UserStr = request.getParameter("user");
String PasswordStr = request.getParameter("password");

// Put your code here
try {
Class.forName("org.gjt.mm.mysql.Driver");
conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test", "root", null);
    stmt = conn.createStatement();
} catch (Exception e) {
e.printStackTrace();
}
String sql = "insert into managers(user,password) values('" + UserStr + "','" + PasswordStr + "')";



try {
stmt = conn.createStatement();
stmt.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
}finally{
try{
if(stmt!=null){
stmt.close();
}
}catch(Exception e){
e.printStackTrace();
}
try{
if(conn!=null){
conn.close();
}
}catch(Exception e1){

}
}

request.getRequestDispatcher("/Add2.jsp").forward(request, response);
   

}

public void destroy() {
}

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





web.xml配置


<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<filter>
<filter-name>t1</filter-name>
<filter-class>sey.Filter1</filter-class>
</filter>
<filter-mapping>
<filter-name>t1</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>






  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>Test1</servlet-name>
    <servlet-class>sey.Test1</servlet-class>
  </servlet>
  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>Test2</servlet-name>
    <servlet-class>sey.Test2</servlet-class>
  </servlet>
  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>Filter1</servlet-name>
    <servlet-class>sey.Filter1</servlet-class>
  </servlet>



  <servlet-mapping>
    <servlet-name>Test1</servlet-name>
    <url-pattern>/servlet/Test1</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>Test2</servlet-name>
    <url-pattern>/servlet/Test2</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>Filter1</servlet-name>
    <url-pattern>/servlet/Filter1</url-pattern>
  </servlet-mapping>
</web-app>

你可能感兴趣的:(java,sql,mysql,jsp,servlet)