前端页面:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <script src="jquery-1.6.1.min.js"></script> <script src="jquery.validate.js"></script> <script src="messages_cn.js"></script> <body style="background-image:url('a19.jpg');background-repeat:no-repeat;"> <div style="margin-left:500px;"> <form action="Server" method="post" name="f1"> <table> <tr> <td align="right">用户名:</td> <td align="left"> <input type="text" name="userName"></input> </td> </tr> <tr> <td align="right">密码:</td> <td align="left"> <input type="password" maxlength="16" id="mima" name="password"></input> </td> </tr> <tr> <td align="right">确认密码:</td> <td align="left"> <input type="password" maxlength="16" equalTo="#mima" name="password1"></input> </td> </tr> <tr> <td align="right">性别:</td> <td align="left" > <input type="radio" name="sex" value="男"/>男 <input type="radio" name="sex" value="女"/>女 </td> </tr> <tr> <td align="right">出生日期:</td> <td align="left"> <input type="date" name="date"></input> </td> </tr> <tr> <td align="right">个人爱好:</td> <td align="left"> <input type="checkbox" name="hobby" value="运动"/>运动 <input type="checkbox" name="hobby" value="睡觉"/>睡觉 <input type="checkbox" name="hobby" value="购物"/>购物 <input type="checkbox" name="hobby" value="上网"/>上网 </td> </tr> <tr> <td colspan="2" align="center"> <input type="submit"/> <input type="reset"/> </td> </tr> </table> </form> </div> </body> </html>
服务器处理:
package com.server; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; 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; /** * Servlet implementation class Server */ @WebServlet("/Server") public class Server extends HttpServlet { private static final long serialVersionUID = 1L; private static String url="jdbc:mysql://localhost:3306/zhuece"; private static String user="root"; private static String password="123456"; public Connection conn=null; public Server() { } static{ try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { e.printStackTrace(); } } public void init(ServletConfig config) throws ServletException { try { conn=DriverManager.getConnection(url,user,password); System.out.println(conn); } catch (SQLException e) { e.printStackTrace(); } } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.getRequestDispatcher("welcome.jsp").forward(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //是对请求的中文内容防止中文乱码 request.setCharacterEncoding("UTF-8"); //对响应的中文的乱码问题 response.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=UTF-8"); //获取用户名: String userName = request.getParameter("userName"); //密码: String password = request.getParameter("password"); //性别: String sexl=request.getParameter("sex"); //生日 String datel=request.getParameter("date"); System.out.println(userName+","+password+","+sexl+","+datel); //爱好数组对象 //String [] hobby = request.getParameterValues("hobby"); String sql="insert into zhuce(username,mima,sex,birthday) values('"+userName+"',"+password+",'"+sexl+"','"+datel+"')"; try { Statement statement = conn.createStatement(); statement.executeUpdate(sql); } catch (SQLException e) { e.printStackTrace(); } request.getRequestDispatcher("denglu.jsp").forward(request, response); } }
跳转1:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <form action="Server" method="get"> <table> <tr> <td align="right">用户名:</td> <td align="left"> <input type="text" name="userName"></input> </td> </tr> <tr> <td align="right">密码:</td> <td align="left"> <input type="password" maxlength="16" id="mima" name="password"></input> </td> </tr> <tr> <td colspan="2" align="center"> <input type="submit"/> <input type="reset"/> </td> </tr> </table> </form> </body> </html>
跳转2:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <p>Welcome !</p> </body> </html>