zoukankan      html  css  js  c++  java
  • 使用请求包装器RequestWrapper 对博客内容进行编码

    1.写一个文章类

    代码如下

    package model;
    
    public class article {
    	private int id;
    	private  String title;
    	private  String content;
    	
    	public article() {
    		super();
    		// TODO 自动生成的构造函数存根
    	}
    
    	public int getId() {
    		return id;
    	}
    
    	public void setId(int id) {
    		this.id = id;
    	}
    
    	public String getTitle() {
    		return title;
    	}
    
    	public void setTitle(String title) {
    		this.title = title;
    	}
    
    	public String getContent() {
    		return content;
    	}
    
    	public void setContent(String content) {
    		this.content = content;
    	}
    
    }
    

     2.写一个数据库操作类

    package dao;
    
    import java.math.BigDecimal;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    import model.article;
    
    public class Addarticle {
    
    	public Addarticle(article article) {
    		String url ="jdbc:mysql://149.129.88.241:3306/A?useUnicode=true&characterEncoding=utf-8";
    		String user="A";
    		String password="888888";
    		
    		try {
    			Class.forName("com.mysql.jdbc.Driver");
    		} catch (ClassNotFoundException e) {
    			// TODO 自动生成的 catch 块
    			e.printStackTrace();
    		}
    		
    		try {
    			Connection connection = DriverManager.getConnection(url,user,password);
    			String sql = "insert into article (title,content) values (?,?)";
    			PreparedStatement statement = connection.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);//预处理sql语句
    			statement.setString(1,article.getTitle());//设置文件名
    			statement.setString(2,article.getContent());//设置输入流
    			
    			statement.executeUpdate();//处理sql语句
    			
    			statement.close();
    		} catch (SQLException e) {
    			// TODO 自动生成的 catch 块
    			e.printStackTrace();
    		}
    	}
    	
    }
    

    3.写一个servlet

    关键代码如下

    		String title=request.getParameter("title");
    		String content=request.getParameter("content");
    		
    		article article = new article();
    		article.setTitle(title);
    		article.setContent(content);
    		
    		Addarticle a= new Addarticle(article);
    

    4.把EscapeFilter.java,和EscapeRequestWrapper.java复制到src的包里面,提交文章后的HTML标签会被编码

    如图所示

  • 相关阅读:
    5_添加购物车 View+Con
    5_添加购物车 B+M
    5_添加购物车 D
    登录注册V
    bootstrap-标题
    h5整理--详解css的相对定位和绝对定位
    各大门户网站的css初始化代码
    九月二十八JS验证
    js函数和运算符
    4.1原始表达式 9/16
  • 原文地址:https://www.cnblogs.com/max-hou/p/11079376.html
Copyright © 2011-2022 走看看