jsp&servlet和JDBC的综合使用

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	<title>Login Page</title>
</head>
<body>
 <%String authority=(String)request.getAttribute("authority");%>
	<form id="login-form" action="Check" method="post">
		<fieldset>
			<legend>Log in</legend>
			<label for="login">用户名</label>
			<input type="text" id="login" name="username" value="<%=null==request.getAttribute("username")?"":request.getAttribute("username") %>"/>
			<div class="clear"></div>
			<label for="password">密码</label>
			<input type="password" id="password" name="password"/>
			<div class="clear"></div>
			<label for="authority">权限</label>
			<div class="clear"></div>
			<label for="remember_me" style="padding: 0;">记住密码</label>
			<input type="checkbox" id="remember_me" style="position: relative; top: 3px; margin: 0; " name="remember_me"/>
			<div class="clear"></div>
			<br/>
			<input type="submit" style="margin: -20px 0 0 287px;" class="button" name="commit" value="登录"/>	
		</fieldset>
	</form>
<br/><br/>
<br/>
<br/>
<br/>
<br/>	
</body>
</html>


以下为servlet代码:

package Servlet;

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

//import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//import javax.servlet.http.HttpSession;

import KarlDoenitz.search;

@SuppressWarnings("serial")
public class Check 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 {

            doPost(request,response);
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		request.setCharacterEncoding("utf-8");
//		HttpSession session=request.getSession();
		PrintWriter out = response.getWriter();
		String username=request.getParameter("username");
		String password=request.getParameter("password");
//		String authority=request.getParameter("authority");
		search s = new search();
		boolean k = s.login(username, password);
		if(k)
		{
			request.getRequestDispatcher("register.jsp").forward(request, response);
		}
		else
		{				  
				 request.getRequestDispatcher("index.jsp").forward(request, response);
		}
		out.flush();
		out.close();	
	}
}
	
另外,导入的login.jar这个包其实就是前面我发表的博客里的jdbc代码打成的,源代码可以去看。

你可能感兴趣的:(jsp,servlet,jdbc,String,search,Class)