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

歡迎光臨散文網(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ó)家法律
滨海县| 孝感市| 南充市| 汽车| 藁城市| 平凉市| 株洲市| 定州市| 大冶市| 中方县| 江华| 金溪县| 城固县| 富蕴县| 高邑县| 伊春市| 康定县| 苍南县| 垦利县| 九台市| 镇康县| 嘉荫县| 西畴县| 资兴市| 上蔡县| 林州市| 正宁县| 游戏| 陆丰市| 江西省| 双鸭山市| 卢湾区| 泰兴市| 广丰县| 龙泉市| 连山| 克什克腾旗| 祁连县| 曲水县| 津南区| 榆树市|