开发总结1

1 表单的验证:
   <script type="text/javascript">
function check()
{
    if (form1.username.value=="")
    {
      alert("请输入用户名!!!");
      form1.username.focus();
      return false;
    }
    if (form1.password.value=="")
    {
      alert("请输入密码!!!");
      form1.password.focus();
      return false;
    }
if (form1.ym.value=="")
    {
      alert("请输入验证码!!!");
      form1.ym.focus();
      return false;
    }
}
</SCRIPT>
在提交是调用:FORM id=form1 method=post  onSubmit="return check()"name=form1 action=login.do>
2 数据库的创建
create table if not exists role_inf
(
id int(20)  auto_increment primary key,
role_id varchar(6) not null unique,
role int(20)  not null default 0,
password varchar(30) not null

);
insert into role_inf values(null,"123456",null,"123455");
实现主键的自动增加
3在Action中如何获得Session:
HttpSession session=request.getSession();
session.setAttribute("user", user);
在页面中显示Session中的内容:
${sessionScope.user.user_id}
4 解决ActionFrom中的中文乱码问题
package filter;
//解决中文乱码问题

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class SetCharacterEncodingFilter implements Filter {

private String encoding;
private FilterConfig filterConfig = null;

public void destroy() {
  this.encoding = null;
  this.filterConfig = null;

}

public void doFilter(ServletRequest request, ServletResponse response,
   FilterChain chain) throws IOException, ServletException {
  request.setCharacterEncoding(this.encoding);
  chain.doFilter(request, response);
}

public void init(FilterConfig filterConfig) throws ServletException {
  this.encoding = filterConfig.getInitParameter("encoding");
}

}
在web.xml中配置fileter
<?xml version="1.0" encoding="gbk"?>
<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">
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
 
  <!-- Standard Action Servlet Configuration (with debugging) -->
  <servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    <init-param>
      <param-name>config</param-name>
      <param-value>/WEB-INF/struts-config.xml</param-value>
    </init-param>
    <init-param>
      <param-name>debug</param-name>
      <param-value>2</param-value>
    </init-param>
    <init-param>
      <param-name>detail</param-name>
      <param-value>2</param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>
  </servlet>


  <!-- Standard Action Servlet Mapping -->
  <servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
 
<!—解决中的乱码问题 -->   <filter>
  <filter-name>Set Character Encoding</filter-name>
  <filter-class>filter.SetCharacterEncodingFilter</filter-class>
  <init-param>
   <param-name>encoding</param-name>
   <param-value>GBK</param-value>
  </init-param>
</filter>

<filter-mapping>
  <filter-name>Set Character Encoding</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>
 
5 解决ActionForm中日期转换问题
在ActionForm中引入import java.sql.Date;
会将输入的日期进行自动转换



</web-app>
6 解决数据库的乱码问题
1》 在建立表的时候设置编码方式:
create table employe_inf
(
id int(20)  auto_increment primary key,
emp_id varchar(6) not null unique,
name varchar(50) not null ,
sex varchar(6)  not null,
born_date date not null,
qq varchar(50)  not null,
  tel varchar(50)  not null,
address varchar(50)  not null

)default character set gbk;
  2》在数据库链接的时候制定编码方式
try {
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost/company?user=root&password=root&useUnicode=true&characterEncoding=GBK");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch(Exception f){f.printStackTrace();}
return con;
}
3》 在mysql中的文件my.ini修改为:default-character-set=gbk

你可能感兴趣的:(mysql,qq,servlet,struts,jdbc)