4月4日 JavaWeb 周三

本文介绍了一个基于Java的简单Web应用实例,包括登录验证、数据增删改查等功能。通过示例代码展示了如何使用JSP页面进行前端展示,以及如何通过Servlet处理HTTP请求并操作数据库。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

登录页面

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'login.jsp' starting page</title>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>

  <body>
    <center>
        <h2>欢迎进入登录页面</h2>
        <form action="/demo15_myproject/login" method="post">
            账号:<input type="text" name="zhang"><br>
            密码:<input type="text" name="mima"><br>
            <input type="submit" value="提交">
        </form>
    </center>
  </body>
</html>

LoginServlet

package com.www.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mysql.jdbc.Driver;

public class LoginServlert extends HttpServlet {


    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String zhangs = request.getParameter("zhang");
        String mimas = request.getParameter("mima");

        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/login", "root", "root");
            String sql ="select * from user03 where id = ? and pwd = ?";
            PreparedStatement pst = con.prepareStatement(sql);

            pst.setString(1, zhangs);
            pst.setString(2, mimas);
            ResultSet rs = pst.executeQuery();

            if(rs.next()){
                System.out.println("登录成功");
                String name = rs.getString(2);
                request.setAttribute("name", name);
                request.getRequestDispatcher("/success.jsp").forward(request, response);
            }else{
                System.out.println("登录失败");
                response.sendRedirect("/demo15_myproject/error.jsp");
            }



        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }


    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        doGet(request, response);
    }

}

success页面

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'success.jsp' starting page</title>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>

  <body>
    <center>
        <h2>登录成功</h2><br>
        <h3>欢迎${name}</h3>
        <h4>您的IP为${str}</h4>
        <a href="/demo15_myproject/show">点击查看列表信息</a>
    </center>
  </body>
</html>

error页面

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'error.jsp' starting page</title>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>

  <body>
    <center>
        <h2>登录失败</h2>
        <a href="/demo15_myproject/login.jsp">返回</a>
    </center>
  </body>
</html>

show页面

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'show.jsp' starting page</title>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>

  <body>
    <center>
        <h1>展示页面</h1>
        <table border="1px" >
        <tr>
            <td>编号</td>
            <td>姓名</td>
            <td>性别</td>
            <td>电话</td>
            <td>班级</td>
            <td>年龄</td>
            <td>工资</td>
            <th><a href="/demo15_myproject/add.jsp">增加</a>(<a href="/demo15_myproject/upd.jsp">修改</a>)</th>
        </tr>
        <c:forEach var="li" items="${list00}">
            <tr>
                <th>${li.eid}</th>
                <th>${li.ename}</th>
                <th>${li.sex}</th>
                <th>${li.tel}</th>
                <th>${li.classid}</th>
                <th>${li.age}</th>
                <th>${li.sal}</th>
                <th><a href="/demo15_myproject/del?w=${li.eid}">删除</a></th>
            </tr>
        </c:forEach>
        </table>
        <a href="/demo15_myproject/bye.jsp">退出</a>
    </center>
  </body>
</html>

showservlet

package com.www.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.www.bean.Elep;

public class ShowServlet extends HttpServlet {


    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {


        List list = new ArrayList();
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/login", "root", "root");
            String sql = "select * from t_temp;";
            PreparedStatement pst = con.prepareStatement(sql);
            ResultSet rs = pst.executeQuery();

            while(rs.next()){
                String eids = rs.getString(1);
                String enames = rs.getString(2);
                String sexs = rs.getString(3);
                String tels = rs.getString(4);
                String classids = rs.getString(5);
                String ages = rs.getString(6);
                String sals = rs.getString(7);
                Elep el = new Elep(eids,enames,sexs,tels,classids,ages,sals);
                list.add(el);
            }

            request.setAttribute("list00", list);
            request.getRequestDispatcher("/show.jsp").forward(request, response);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }


    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        doGet(request, response);
    }

}

add页面

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'add.jsp' starting page</title>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>

  <body>
    <center>
        <form action="/demo15_myproject/add" method="post">
            账号:<input type="text" name="zhang"><br>
            姓名:<input type="text" name="ming"><br>
            性别:<input type="text" name="bie"><br>
            电话:<input type="text" name="dian"><br>
            单位:<input type="text" name="dan"><br>
            年龄:<input type="text" name="nian"><br>
            工资:<input type="text" name="gong"><br>
            <input type="submit" value="添加">
        </form>
    </center>
  </body>
</html>

addservlet

package com.www.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class AddServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {


            String zhangs = request.getParameter("zhang");
            String mings = request.getParameter("ming");
            String bies = request.getParameter("bie");
            String dians = request.getParameter("dian");
            String dans = request.getParameter("dan");
            String nians = request.getParameter("nian");
            String gongs = request.getParameter("gong");

        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/login", "root", "root");
            String sql = "insert into t_temp values(?,?,?,?,?,?,?);";
            PreparedStatement pst = con.prepareStatement(sql);
            pst.setString(1, zhangs);
            pst.setString(2, mings);
            pst.setString(3, bies);
            pst.setString(4, dians);
            pst.setString(5, dans);
            pst.setString(6, nians);
            pst.setString(7, gongs);

            int x = pst.executeUpdate();
            if(x>0){
                response.sendRedirect("/demo15_myproject/show");
            }

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }


    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        doGet(request, response);
    }

}

delservlet

package com.www.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class DelServlet extends HttpServlet {


    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String shan = request.getParameter("w");

        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/login", "root", "root");
            String sql = "delete from t_temp where eid = ?";
            PreparedStatement pst = con.prepareStatement(sql);

            pst.setString(1, shan);

            int x = pst.executeUpdate();

            if(x>0){
                response.sendRedirect("/demo15_myproject/show");
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }


    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        doGet(request, response);
    }

}

upd页面

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'upd.jsp' starting page</title>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>

  <body>
    <center>
        <form action="/demo15_myproject/upd" method="post">
            账号:<input type="text" name="zhangu"><br>
            姓名:<input type="text" name="mingu"><br>
            性别:<input type="text" name="bieu"><br>
            电话:<input type="text" name="dianu"><br>
            单位:<input type="text" name="danu"><br>
            年龄:<input type="text" name="nianu"><br>
            工资:<input type="text" name="gongu"><br>
            <input type="submit" value="修改">
        </form>
    </center>    
  </body>
</html>

updservlet

package com.www.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class UpdServlet extends HttpServlet {


    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String mingu = request.getParameter("mingu");
        String bieu = request.getParameter("bieu");
        String dianu = request.getParameter("dianu");
        String danu = request.getParameter("danu");
        String nianu = request.getParameter("nianu");
        String gongu = request.getParameter("gongu");

        String zhangu = request.getParameter("zhangu");

        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/login", "root", "root");
            String sql = "update t_temp set ename=?,sex=?,tel=?,classid=?,age=?,sal=? where eid=?;";
            PreparedStatement pst = con.prepareStatement(sql);

            pst.setString(1, mingu);
            pst.setString(2, bieu);
            pst.setString(3, dianu);
            pst.setString(4, danu);
            pst.setString(5, nianu);
            pst.setString(6, gongu);
            pst.setString(7, zhangu);

            int x = pst.executeUpdate();

            if(x>0){
                response.sendRedirect("/demo15_myproject/show");
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        doGet(request, response);
    }

}

bye页面

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'bye.jsp' starting page</title>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>

  <body>
    <center>
        <h1>再见</h1>
    </center>
  </body>
</html>

elep

package com.www.bean;

public class Elep {
    //属性
    String eid;
    String ename;
    String sex;
    String tel;
    String classid;
    String age;
    String sal;
    //构造方法
    public Elep(String eid, String ename, String sex, String tel,
            String classid, String age, String sal) {
        super();
        this.eid = eid;
        this.ename = ename;
        this.sex = sex;
        this.tel = tel;
        this.classid = classid;
        this.age = age;
        this.sal = sal;
    }
    //设值取值
    public String getEid() {
        return eid;
    }
    public void setEid(String eid) {
        this.eid = eid;
    }
    public String getEname() {
        return ename;
    }
    public void setEname(String ename) {
        this.ename = ename;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public String getTel() {
        return tel;
    }
    public void setTel(String tel) {
        this.tel = tel;
    }
    public String getClassid() {
        return classid;
    }
    public void setClassid(String classid) {
        this.classid = classid;
    }
    public String getAge() {
        return age;
    }
    public void setAge(String age) {
        this.age = age;
    }
    public String getSal() {
        return sal;
    }
    public void setSal(String sal) {
        this.sal = sal;
    }


}

MyFilter

package com.www.Filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class MyFilter implements Filter{

    public void destroy() {
        // TODO Auto-generated method stub
        System.out.println("过滤器销毁");
    }

    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {
        // TODO Auto-generated method stub
        System.out.println("过滤器运行");

        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        String str1 = request.getLocalAddr();//本地
        String str = request.getRemoteAddr();
        request.setAttribute("str", str);

        chain.doFilter(request, response);
        System.out.println(str1);
    }

    public void init(FilterConfig arg0) throws ServletException {
        // TODO Auto-generated method stub
        System.out.println("过滤器启动");
    }

}

读书是学习,使用也是学习,而且是更重要的学习。
——毛泽东

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值