JavaWeb(Servlet预习)

案例1:基于jsp+Servlet实现用户登录验证

1.input.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

	<form action="loginCheck" method="post">
		用户名:<input type="text" name="username" /><br> 密码:<input
			type="password" name="userpwd" /><br> <input type="submit"
			value="提交"> <input type="reset">
	</form>
</body>
</html>

2.loginCheckServlet.java

package servlets;

import java.io.IOException;

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

@WebServlet("/loginCheck")
public class LoginCheckServlet extends HttpServlet {
	public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
		String userName = request.getParameter("username");
		String userPwd = request.getParameter("userpwd");
		String info = "";
		if("abc".equals(userName)&&"123".equals(userPwd)) {
			info="欢迎你"+userName;
		}else {
			info="用户名或密码错误";
		}
		request.setAttribute("outputMessage", info);
		request.getRequestDispatcher("/info.jsp").forward(request, response);
	}
	
	
}

3.info.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%=request.getAttribute("outputMessage") %>
</body>
</html>

案例2:基于jsp+Servlet+JavaBean实现用户注册

1.数据库连接类

package db;

import java.sql.*;

public class JdbcUtil {
	public static Connection getConnection() throws Exception{
		String driverName = "com.mysql.jdbc.Driver";
		String dbName = "students";
		String userName = "root";
		String userPwd = "1234";
		String url1 = "jdbc:mysql://localhost:3306/"+dbName;
		String url2 = "?user="+userName+"&password="+userPwd;
		String url3 = "&useUnicode=true&characterEncoding=UTF-8";
		String url = url1+url2+url3;
		
		Class.forName(driverName);
		Connection conn = DriverManager.getConnection(url);
		return conn;
	}
	
	public static void free(ResultSet rs,PreparedStatement pstmt,Connection conn) throws SQLException {
		if(rs!=null) {
			rs.close();
		}
		if(pstmt!=null) {
			pstmt.close();
		}
		if(conn!=null) {
			conn.close();
		}
	}
}

2.javabean实体类

package beans;

public class User {
	private String userName;
	private String userPwd;
	public User(String userName,String userPwd) {
		this.userName = userName;
		this.userPwd = userPwd;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getUserPwd() {
		return userPwd;
	}
	public void setUserPwd(String userPwd) {
		this.userPwd = userPwd;
	}
	public User() {}
	
}

3.数据库访问类dao

package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import javax.swing.plaf.basic.BasicInternalFrameTitlePane.RestoreAction;

import beans.User;
import db.JdbcUtil;

public class UserDao {
	
	//添加
	public void add(User user) throws Exception{
		Connection conn = null;
		PreparedStatement pstmt = null;
		conn = JdbcUtil.getConnection();
		String sql = "insert into user_b(username,userpassword) values(?,?)";
		pstmt = conn.prepareStatement(sql);
		pstmt.setString(1, user.getUserName());
		pstmt.setString(2, user.getUserPwd());
		pstmt.executeUpdate();
		JdbcUtil.free(null, pstmt, conn);
	}
	
	//查询全部
	public List<User> QueryAll() throws Exception{
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		List<User> UserList = new ArrayList<User>();
		conn = JdbcUtil.getConnection();
		String sql = "select * from user_b";
		pstmt = conn.prepareStatement(sql);
		rs = pstmt.executeQuery();
		while(rs.next()) {
			String xm = rs.getString("username");
			String mm = rs.getString("password");
			User user = new User(xm,mm);
			UserList.add(user);
		}
		JdbcUtil.free(rs, pstmt, conn);
		return UserList;
		
	}
	
	//修改
	public int update(User user) throws Exception{
		Connection conn = null;
		PreparedStatement pstmt = null;
		int result = 0;
		conn = JdbcUtil.getConnection();
		String sql = "update user_b set userpassword=? where username=?";
		pstmt.setString(1, user.getUserName());
		pstmt.setString(2, user.getUserPwd());
		result = pstmt.executeUpdate();
		JdbcUtil.free(null, pstmt, conn);
		return result;
	}
	
	//删除
	public int delete(int username) throws Exception{
		Connection conn = null;
		PreparedStatement pstmt = null;
		int result = 0;
		conn = JdbcUtil.getConnection();
		String sql = "delete from user_b where username=?";
		pstmt = conn.prepareStatement(sql);
		pstmt.setInt(1, username);
		result = pstmt.executeUpdate();
		JdbcUtil.free(null, pstmt, conn);
		return result;
	}
	
}






4.注册页面a.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form>
		用户名:<input type="text" name="xm"><br>
		<br> 密码:<input type="password" name="mm"><br>
		<br> <input type="submit" value="提交">
	</form>
</body>
</html>

5.处理登录的servlet

6.结果页面b.jsp

例3:学生体质信息管理

student.java

package beans;

public class Student {

	private int id;
	private String name;
	private String sex;
	private int age;
	private float weight;
	private float height;

	public Student() {}
	public Student(int id,String name,String sex,int age,float weight,float height) {
		this.id = id;
		this.name = name;
		this.sex = sex;
		this.age = age;
		this.weight = weight;
		this.height = height;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public float getWeight() {
		return weight;
	}
	public void setWeight(float weight) {
		this.weight = weight;
	}
	public float getHeight() {
		return height;
	}
	public void setHeight(float height) {
		this.height = height;
	}
	
	
}

studentDao.java

package Dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import org.apache.tomcat.dbcp.dbcp2.PStmtKey;

import com.sun.org.apache.xerces.internal.util.EntityResolver2Wrapper;

import Util.JdbcUtil;
import beans.Student;

public class StudentDao {
	
	public Student create(Student stu) throws Exception{
		Connection conn = JdbcUtil.getConnection();
		String sql = "insert into stu_info (id,name,sex,age,weight,height) values(?,?,?,?,?,?)";
		PreparedStatement pstmt = conn.prepareStatement(sql);
		pstmt.setInt(1, stu.getId());
		pstmt.setString(2, stu.getName());
		pstmt.setString(3, stu.getSex());
		pstmt.setInt(4, stu.getAge());
		pstmt.setFloat(5, stu.getWeight());
		pstmt.setFloat(6, stu.getHeight());
		pstmt.executeUpdate();
		JdbcUtil.free(null, conn, pstmt);
		return stu;
	}
	
	public List<Student> findAll() throws Exception{
		Connection conn = JdbcUtil.getConnection();
		String sql = "select * from stu_info";
		PreparedStatement pstmt = conn.prepareStatement(sql);
		ResultSet rs = null;
		List<Student> students = new ArrayList<Student>();
		rs = pstmt.executeQuery();
		while(rs.next()) {
			Student stu2 = new Student();
			stu2.setId(rs.getInt(1));
			stu2.setName(rs.getString(2));
			stu2.setSex(rs.getString(3));
			stu2.setAge(rs.getInt(4));
			stu2.setWeight(rs.getFloat(5));
			stu2.setHeight(rs.getFloat(6));
			students.add(stu2);
		}
		JdbcUtil.free(rs, conn, pstmt);
		return students;
		
	}
	
	public void remove(Student stu) throws Exception{
		Connection conn = JdbcUtil.getConnection();
		String sql = "delete from stu_info where name=?";
		PreparedStatement pstmt = conn.prepareStatement(sql);
		pstmt.setString(1, stu.getName());
		pstmt.executeUpdate();
		JdbcUtil.free(null, conn, pstmt);
	}
	
	public void update(Student stu) throws Exception{
		Connection conn = JdbcUtil.getConnection();
		String sql = "update stu_info set id=?,name=?,sex=?,age=?,weight=?,height=? where name=? ";
		PreparedStatement pstmt = conn.prepareStatement(sql);
		pstmt.setInt(1, stu.getId());
		pstmt.setString(2, stu.getName());
		pstmt.setString(3, stu.getSex());
		pstmt.setInt(4, stu.getAge());
		pstmt.setFloat(5, stu.getWeight());
		pstmt.setFloat(6, stu.getHeight());
		pstmt.setString(7, stu.getName());
		pstmt.executeUpdate();
		JdbcUtil.free(null, conn, pstmt);
		
	}
}





JdbcUtil.java

package Util;

import java.sql.*;
import java.sql.DriverManager;

import com.mysql.cj.jdbc.Driver;

public class JdbcUtil {
	public static Connection getConnection() throws Exception {
		String dbName = "students";
		String driverName = "com.mysql.jdbc.Driver";
		String userName = "root";
		String userPwd = "1234";
		String url1 = "jdbc:mysql://localhost:3306/"+dbName;
		String url2 = "?user="+userName+"&password"+userPwd;
		String url3 = "&useUnicode=true&characterEncoding=UTF-8";
		String url = url1+url2+url3;
		
		Class.forName(driverName);
		Connection conn = DriverManager.getConnection(url);
		return conn;
	}
	
	
	public static void free(ResultSet rs,Connection conn,PreparedStatement pstmt) throws Exception {
		if(rs!=null) {
			rs.close();
		}
		if(conn!=null) {
			conn.close();
		}
		if(pstmt!=null) {
			pstmt.close();
		}
	}
	
	
}

servlet:   insert.java

package servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Dao.StudentDao;
import beans.Student;

/**
 * Servlet implementation class Insert
 */
@WebServlet("/Insert")
public class Insert extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Insert() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		int id = Integer.parseInt(request.getParameter("id"));
		String name = request.getParameter("name");
		String sex = request.getParameter("sex");
		int age = Integer.parseInt(request.getParameter("age"));
		Float weight = Float.parseFloat(request.getParameter("weight"));
		Float height = Float.parseFloat("height");
		
		Student stu = new Student(id,name,sex,age,weight,height);
		
		StudentDao studentDao = new StudentDao();
		studentDao.create(stu);
		response.sendRedirect(StudentServlet?action=list);
		
		
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

list.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8" import="java.util.*" import="beans.Student"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>学生信息管理</title>
</head>
<body>
	<a href="insert.jsp">添加学生</a>

	<table>
		<tr>
			<th>学号</th>
			<th>姓名</th>
			<th>性别</th>
			<th>年龄</th>
			<th>体重</th>
			<th>身高</th>
			<th>操作</th>
		</tr>
		<%
			List<Student> students = (List<Student>) request.getAttribute("students");
			if (students != null && !students.isEmpty()) {
				for (Student student : students) {
		%>
		<tr>
			<td><%=student.getId()%></td>
			<td><%=student.getName()%></td>
			<td><%=student.getSex()%></td>
			<td><%=student.getAge()%></td>
			<td><%=student.getWeight()%></td>
			<td><%=student.getHeight()%></td>
			<td><a href="edit.jsp?name=<%=student.getName()%>">编辑</a> <a
				href="delete.jsp?name=<%=student.getName()%>"
				onclick="return confirm('确定要删除吗?')">删除</a></td>
		</tr>

		<%
			}
		}
		%>


	</table>
</body>
</html>

insert.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form action="insert" method="post">
		<table>
			<tr>
				<td>学号</td>
				<td><input type="text" name="id"></td>
			</tr>
			<tr>
				<td>姓名</td>
				<td><input type="text" name="name"></td>
			</tr>
			<tr>
				<td>性别</td>
				<td><input type="text" name="sex"></td>
			</tr>
			<tr>
				<td>年龄</td>
				<td><input type="text" name="age"></td>
			</tr>
			<tr>
				<td>体重</td>
				<td><input type="text" name="weight"></td>
			</tr>
			<tr>
				<td>身高</td>
				<td><input type="text" name="height"></td>
			</tr>
			<tr>
				<td><input type="submit" value="提交"></td>
			</tr>

		</table>

	</form>
</body>
</html>

内容概要:本文介绍了一种基于蒙特卡洛模拟和拉格朗日优化方法的电动汽车充电站有序充电调度策略,重点针对分时电价机制下的分散式优化问题。通过Matlab代码实现,构建了考虑用户充电需求、电网负荷平衡及电价波动的数学模【电动汽车充电站有序充电调度的分散式优化】基于蒙特卡诺和拉格朗日的电动汽车优化调度(分时电价调度)(Matlab代码实现)型,采用拉格朗日乘子法处理约束条件,结合蒙特卡洛方法模拟大量电动汽车的随机充电行为,实现对充电功率和时间的优化分配,旨在降低用户充电成本、平抑电网峰谷差并提升充电站运营效率。该方法体现了智能优化算法在电力系统调度中的实际应用价值。; 适合人群:具备一定电力系统基础知识和Matlab编程能力的研究生、科研人员及从事新能源汽车、智能电网相关领域的工程技术人员。; 使用场景及目标:①研究电动汽车有序充电调度策略的设计与仿真;②学习蒙特卡洛模拟与拉格朗日优化在能源系统中的联合应用;③掌握基于分时电价的需求响应优化建模方法;④为微电网、充电站运营管理提供技术支持和决策参考。; 阅读建议:建议读者结合Matlab代码深入理解算法实现细节,重点关注目标函数构建、约束条件处理及优化求解过程,可尝试调整参数设置以观察不同场景下的调度效果,进一步拓展至多目标优化或多类型负荷协调调度的研究。
内容概要:本文围绕面向制造业的鲁棒机器学习集成计算流程展开研究,提出了一套基于Python实现的综合性计算框架,旨在应对制造过程中数据不确定性、噪声干扰面向制造业的鲁棒机器学习集成计算流程研究(Python代码实现)及模型泛化能力不足等问题。该流程集成了数据预处理、特征工程、异常检测、模型训练与优化、鲁棒性增强及结果可视化等关键环节,结合集成学习方法提升预测精度与稳定性,适用于质量控制、设备故障预警、工艺参数优化等典型制造场景。文中通过实际案例验证了所提方法在提升模型鲁棒性和预测性能方面的有效性。; 适合人群:具备Python编程基础和机器学习基础知识,从事智能制造、工业数据分析及相关领域研究的研发人员与工程技术人员,尤其适合工作1-3年希望将机器学习应用于实际制造系统的开发者。; 使用场景及目标:①在制造环境中构建抗干扰能力强、稳定性高的预测模型;②实现对生产过程中的关键指标(如产品质量、设备状态)进行精准监控与预测;③提升传统制造系统向智能化转型过程中的数据驱动决策能力。; 阅读建议:建议读者结合文中提供的Python代码实例,逐步复现整个计算流程,并针对自身业务场景进行数据适配与模型调优,重点关注鲁棒性设计与集成策略的应用,以充分发挥该框架在复杂工业环境下的优势。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值