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代码打成的,源代码可以去看。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值