4月3日 JavaWeb 周二

WEB

获取表单提交数据
request.getParameter();
给网页作出响应
response.getWriter().write();
重定向
response.sendRedirect();
转发
request.setAttribute("","");
request.getRequestDispatcher("").forward(request,response);
转发的时候路径不加/ 重定向必须加
处理请求乱码
request.setCharacterEcoding("utf-8");
处理响应乱码
response.setCharacterEcoding("utf-8");
response.setContentType("text/html;charset=utf-8");

JSP

三大指令
1`page
2`include
3`taglib

九大内置对象
1`request
2`response
3`session
4`application
5`pageContext
6`page
7`out
8`exception
9`config

四大域对象
1`pageContext
2`request
3`session
4`applicatoin

六大动作标签
1`forward
2`include
3`param
4`useBean
5`setProperty
6`getProperty

login界面

<%@ 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>
        <h3>登录界面</h3>
        <form action="/demo11_exam/login" method="post">
            账号:<input type="text" name="zhang"><br>
            密码:<input type="text" name="mima"><br>
            <hr>
            <input type="submit" value="登录">
        </form>
        ${sb}
    </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 LoginServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //处理乱码
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        //获取表单数据
        String zhangs = request.getParameter("zhang");
        String mimas = request.getParameter("mima");
        //JDBC
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/yyy", "root", "root");
            String sql = "select * from t_temp where id=? and password=?";
            PreparedStatement pst = con.prepareStatement(sql);

            pst.setString(1, zhangs);
            pst.setString(2, mimas);

            ResultSet rs = pst.executeQuery();
            if(rs.next()){
                System.out.println("登录成功");
                response.sendRedirect("/demo11_exam/show");
            }else{
                System.out.println("登录失败");
                request.setAttribute("sb", "账号或密码错误,请重新输入");
                request.getRequestDispatcher("/login.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);
    }

}

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 {
        //处理乱码
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        //JDBC
        List list = new ArrayList();
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/yyy", "root", "root");
            String sql = "select * from t_temp ;";
            PreparedStatement pst = con.prepareStatement(sql);
            ResultSet rs = pst.executeQuery();

            while(rs.next()){
                String ids = rs.getString(1);
                String names = rs.getString(2);
                String passs = rs.getString(3);
                String sexs = rs.getString(4);
                String ages = rs.getString(5);
                Elep el = new Elep(ids,names,passs,sexs,ages);
                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);
    }

}

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>
    <h2>This is show page</h2>
    <table border="2px">
    <tr>
            <td>
            账号
            </td>
            <td>
            姓名
            </td>
            <td>
            密码
            </td>
            <td>
            性别
            </td>
            <td>
            年龄
            </td>
            <td>
            <a href="/demo11_exam/add.jsp">添加</a>
            </td>
        </tr>
    <c:forEach var="li" items="${list00}">
        <tr>
            <td>
            ${li.id}
            </td>
            <td>
            ${li.name}
            </td>
            <td>
            ${li.password}
            </td>
            <td>
            ${li.sex}
            </td>
            <td>
            ${li.age}
            </td>
        </tr>
    </c:forEach>
    </table>
  </center>
  </body>
</html>

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="/demo11_exam/add" method="post">
            账号:<input type="text" name="hao"><br>
            姓名:<input type="text" name="ming"><br>
            密码:<input type="text" name="mima"><br>
            性别:<input type="text" name="bie"><br>
            年龄:<input type="text" name="nian"><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 java.sql.ResultSet;

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 {
        //处理乱码
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        //获取表单数据
        String haos = request.getParameter("hao");
        String mings = request.getParameter("ming");
        String mimas = request.getParameter("mima");
        String bies = request.getParameter("bie");
        String nians = request.getParameter("nian");
        //JDBC
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/yyy", "root", "root");
            String sql = "insert into t_temp values(?,?,?,?,?);";
            PreparedStatement pst = con.prepareStatement(sql);

            pst.setString(1, haos);
            pst.setString(2, mings);
            pst.setString(3, mimas);
            pst.setString(4, bies);
            pst.setString(5, nians);

            int x = pst.executeUpdate();
            if(x>0){
                System.out.println("添加成功");
                response.sendRedirect("/demo11_exam/show");
            }else{
                response.sendRedirect("/demo11_exam/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);
    }

}

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>
    </center>
  </body>
</html>

Elep对象

package com.www.bean;

public class Elep {
    String id;
    String name;
    String password;
    String sex;
    String age;

    public Elep(String id, String name, String password, String sex, String age) {
        super();
        this.id = id;
        this.name = name;
        this.password = password;
        this.sex = sex;
        this.age = age;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }


}

万物皆下品,惟有读书高

Python网络爬虫与推荐算法新闻推荐平台:网络爬虫:通过Python实现新浪新闻的爬取,可爬取新闻页面上的标题、文本、图片、视频链接(保留排版) 推荐算法:权重衰减+标签推荐+区域推荐+热点推荐.zip项目工程资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松复刻,拿到资料包后可轻松复现出一样的项目,本人系统开发经验充足(全领域),有任何使用问题欢迎随时与我联系,我会及时为您解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(如有)等。答辩评审平均分达到96分,放心下载使用!可轻松复现,设计报告也可借鉴此项目,该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的。 【提供帮助】:有任何使用问题欢迎随时与我联系,我会及时解答解惑,提供帮助 【附带帮助】:若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步 【项目价值】:可用在相关项目设计中,皆可应用在项目、毕业设计、课程设计、期末/期中/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面,可借鉴此优质项目实现复刻,设计报告也可借鉴此项目,也可基于此项目来扩展开发出更多功能 下载后请首先打开README文件(如有),项目工程可直接复现复刻,如果基础还行,也可在此程序基础上进行修改,以实现其它功能。供开源学习/技术交流/学习参考,勿用于商业用途。质量优质,放心下载使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值