注册页面跳转登录页面
package org.chenao;import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;public class Login extends HttpServlet {/*** The doGet method of the servlet. <br>** This method is called when a form has its tag value method equals to get.* * @param request the request send by the client to the server* @param response the response send by the server to the client* @throws ServletException if an error occurred* @throws IOException if an error occurred*/public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setContentType("text/html");PrintWriter out = response.getWriter();out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");out.println("<HTML>");out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");out.println(" <BODY>");out.print(" This is ");out.print(this.getClass());out.println(", using the GET method");out.println(" </BODY>");out.println("</HTML>");out.flush();out.close();}/*** The doPost method of the servlet. <br>** This method is called when a form has its tag value method equals to post.* * @param request the request send by the client to the server* @param response the response send by the server to the client* @throws ServletException if an error occurred* @throws IOException if an error occurred*/public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {request.setCharacterEncoding("utf-8");response.setCharacterEncoding("utf-8");response.setContentType("text/html");String pwd =request.getParameter("pwd");String userName= request.getParameter("userName");boolean isTrue = false;Connection conn= DBUtil.getConn();String sql ="select * from users where user_name = ? and pwd = ?";PreparedStatement ps=null;ResultSet rs= null;try {ps=conn.prepareStatement(sql);ps.setString(1, userName);ps.setString(2, pwd);rs=ps.executeQuery();if (rs.next()) {isTrue=true;} else {isTrue=false;}} catch (SQLException e) {e.printStackTrace();}DBUtil.close(conn,ps,null);PrintWriter out = response.getWriter();out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");out.println("<HTML>");out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");out.println(" <BODY>");if (isTrue) {System.out.println("<h1 style='color:green'>注册成功</h1>");System.out.println("a href='index.jsp'>已有账户,登录</a>");} else {System.out.println("<h1 style='color:red'>注册失敗</h1>");}out.println(" </BODY>");out.println("</HTML>");out.flush();out.close();}}
登录页面跳转注册页面
package org.chenao;import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;public class TestServlet extends HttpServlet {/*** The doGet method of the servlet. <br>** This method is called when a form has its tag value method equals to get.* * @param request the request send by the client to the server* @param response the response send by the server to the client* @throws ServletException if an error occurred* @throws IOException if an error occurred*/public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setContentType("text/html");PrintWriter out = response.getWriter();out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");out.println("<HTML>");out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");out.println(" <BODY>");out.print(" This is ");out.print(this.getClass());out.println(", using the GET method");out.println(" </BODY>");out.println("</HTML>");out.flush();out.close();}/*** The doPost method of the servlet. <br>** This method is called when a form has its tag value method equals to post.* * @param request the request send by the client to the server* @param response the response send by the server to the client* @throws ServletException if an error occurred* @throws IOException if an error occurred*/public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {request.setCharacterEncoding("utf-8");response.setCharacterEncoding("utf-8");response.setContentType("text/html");String pwd =request.getParameter("pwd");String userName= request.getParameter("userName");boolean isTrue = false;Connection conn= DBUtil.getConn();String sql ="select * from users where user_name = ? and pwd = ?";PreparedStatement ps=null;ResultSet rs= null;try {ps=conn.prepareStatement(sql);ps.setString(1, userName);ps.setString(2, pwd);rs=ps.executeQuery();if (rs.next()) {isTrue=true;} else {isTrue=false;}} catch (SQLException e) {e.printStackTrace();}DBUtil.close(conn,ps,null);PrintWriter out = response.getWriter();out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");out.println("<HTML>");out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");out.println(" <BODY>");if (isTrue) {System.out.println("a href='info.jsp'>没有账户,点击注册</a>");} else {System.out.println("<h1 style='color:red'>登录成功</h1>");}out.println(" </BODY>");out.println("</HTML>");out.flush();out.close();}}
DBUtil部分页面
package org.chenao;import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;public class DBUtil {static{try {Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");} catch (ClassNotFoundException e) {e.printStackTrace();}}public static Connection getConn(){Connection conn = null;try {conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName= MyDB","sa","1");} catch (SQLException e) {e.printStackTrace();}return conn;}public static void close(Connection conn,PreparedStatement ps,ResultSet rs) {try {if(conn != null)conn.close();} catch (SQLException e) {e.printStackTrace();}try {if(ps != null)conn.close();} catch (SQLException e) {e.printStackTrace();}try {if(rs != null)conn.close();} catch (SQLException e) {e.printStackTrace();}}}
登录页面
<body>用户名:<input type="text" value="userName"/><br/>密 码:<input type="password" value="name"/><br/><input type="submit" value="登录" /><a href="info.jsp"><input type="submit" value="没有账号,请注册"/></a></body>
注册页面
<body>姓名:<input type="text" value="name"/><br/>密码:<input type="password" value="name"/><br/>确认密码:<input type="password" value="name"/><br/>用户名:<input type="text" value="userName"/><br/>年龄:<input type="text" value="age"/><br/><input type="submit" value="注册" /></body>
页面跳转按钮
<a href='跳转页面的文件名'></a>