最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會(huì)員登陸 & 注冊(cè)

javabean+servlet應(yīng)用

2023-06-24 23:19 作者:G7oose  | 我要投稿

login.jsp


<%@ page language="java" isErrorPage="true" pageEncoding="utf-8"%>

<!DOCTYPE html>


<head>

<meta charset="utf-8">

<title></title>

<link rel="stylesheet" type="text/css" href="style.css" />

</head>

<body>

<div class="container">

? ? <section id="content">

? ? ? ? <form action="${pageContext.request.contextPath}/Servlet/Userservlet?method=login" method="post"><!---${pageContext.request.contextPath}的目的是找到主工程的名字,/Servlet/Userservlet就是web.xml下面配置的路徑,也就是servlet的路徑--->


? ? ? ? ? ? <h1>登錄 界面</h1>

? ? ? ? ? ? <div>

? ? ? ? ? ? ? ? <input type="text" placeholder="登錄名" required="" id="username" name="username1"/>

? ? ? ? ? ? </div>

? ? ? ? ? ? <div>

? ? ? ? ? ? ? ? <input type="password" placeholder="密碼" required="" id="password" name="password1" />

? ? ? ? ? ? </div>

? ? ? ? ? ? <div>

? ? ? ? ? ? ? ? <input type="submit" value="登 錄" />

? ? ? ? ? ? ? ? <a href="#">注冊(cè)</a>

? ? ? ? ? ? </div>

? ? ? ? </form><!-- form -->

? ? ? ? <div class="button">

? ? ? ? </div><!-- button -->

? ? </section><!-- content -->

</div><!-- container -->

</body>

</html>



userbean.java


package bean;


public class Userbean {

? ? private String username;//用戶名

? ? private String password;//密碼都是與數(shù)據(jù)庫(kù)匹配的,下面是set和get函數(shù)

? ? public String getUsername() {

? ? ? ? return username;

? ? }

? ? public void setUsername(String username) {

? ? ? ? this.username = username;

? ? }

? ? public String getPassword() {

? ? ? ? return password;

? ? }

? ? public void setPassword(String password) {

? ? ? ? this.password = password;

? ? }

? ??


}


userservlet.java



package servlet;


import java.io.IOException;


import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;


import dao.Userdao;




@SuppressWarnings("serial")

public class Userservlet extends HttpServlet

{

? ? //當(dāng)從jsp跳轉(zhuǎn)到servlet類時(shí),首先執(zhí)行service函數(shù)(這是定理)

? ? protected void service(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException

? ? {

? ? ? ? req.setCharacterEncoding("utf-8");//設(shè)置字符集,避免亂碼

? ? ? ? //獲取jsp界面需要進(jìn)行的操作,

? ? ? ? String method = req.getParameter("method");

? ? ? ? if(method.equals("login"))//轉(zhuǎn)到login函數(shù)

? ? ? ? {

? ? ? ? ? ? login(req,resp);

? ? ? ? }

? ? }

? ? protected void login(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException

? ? {

? ? ? ? req.setCharacterEncoding("utf-8");

? ? ? ? String username = req.getParameter("username1");//獲取jsp界面的username1和password1的值

? ? ? ? String password = req.getParameter("password1");

? ? ? ? Userdao userdao = new Userdao();//創(chuàng)建Userdao的實(shí)例

? ? ? ? int flag = userdao.login(username, password);//用來(lái)判斷是否登陸成功

? ? ? ? if(flag==1)

? ? ? ? {

? ? ? ? ? ? System.out.println("登錄成功!");

? ? ? ? ? ? resp.sendRedirect(req.getContextPath()+"/index.jsp");

? ? ? ? }

? ? ? ? else

? ? ? ? {

? ? ? ? ? ? System.out.println("登錄失?。?#34;);

? ? ? ? }

? ? }


}




userdao.java



package dao;


import java.sql.Connection;

import java.sql.ResultSet;

import java.sql.Statement;


import db.DBUtil;


public class Userdao {

? ? public int login(String username,String password)

? ? {

? ? ? ? Connection conn = DBUtil.getConn();//這里就是從DBUtil類里面得到連接

? ? ? ? Statement state =null;

? ? ? ? ResultSet rs = null;

? ? ? ? int flag=0;

? ? ? ? try

? ? ? ? {

? ? ? ? ? ? String sql = "select * from user where name = '"+username+"'";//SQL語(yǔ)句,

? ? ? ? ? ? state = conn.createStatement();

? ? ? ? ? ??

? ? ? ? ? ? rs=state.executeQuery(sql);

? ? ? ? ? ? if(rs.next())

? ? ? ? ? ? {

? ? ? ? ? ? ? ? if(rs.getString("password").equals(password))

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? flag=1;

? ? ? ? ? ? ? ? }

? ? ? ? ? ? }

? ? ? ? }

? ? ? ? catch(Exception e)

? ? ? ? {

? ? ? ? ? ? e.printStackTrace();

? ? ? ? }

? ? ? ??

? ? ? ? finally

? ? ? ? {

? ? ? ? ? ? DBUtil.close(rs, state, conn);

? ? ? ? }

? ? ? ? return flag;

? ? }

? ??


}


javabean+servlet應(yīng)用的評(píng)論 (共 條)

分享到微博請(qǐng)遵守國(guó)家法律
陇西县| 新丰县| 红河县| 星座| 定结县| 德江县| 志丹县| 繁昌县| 渝中区| 左权县| 砀山县| 梁平县| 洛川县| 东港市| 简阳市| 建昌县| 利辛县| 四会市| 新津县| 阆中市| 嘉兴市| 阿克| 夏河县| 海安县| 临安市| 阿瓦提县| 电白县| 东平县| 峨眉山市| 丰镇市| 天津市| 马关县| 胶州市| 平凉市| 临朐县| 鹤岗市| 济源市| 武安市| 山阳县| 陇川县| 昌都县|