JSP语言基础(案例代码)

 JSP基本语法

编写一个JSP页面,在该页面中显示当前时间

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="java.util.*"%>
<%@ page import="java.text.SimpleDateFormat"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>一个简单的JSP页面——显示系统时间</title>
</head>
<body>
<%Date date=new Date();SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");String today=df.format(date);
%>当前时间:<%=today %></body>
</html>

5267b556194446498fc9c192544878c0.png

 

应用include指令包含网站Banner和版权信息栏

<%@ page pageEncoding="GB18030"%>
<img src="images/banner.JPG">
<%@ page pageEncoding="GB18030"%>
<%
String copyright="&nbsp;ALL Copyright &copy;2009 有限公司";
%>
<table width="778" height="61" border="0" cellpadding="0"
cellspacing="0" background="images/copyright.JPG"><tr><td> <%=copyright %></td></tr>
</table>
<%@ page language="java" contentType="text/html; charset=GB18030"pageEncoding="GB18030"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>使用文件包含include指令</title>
</head>
<body style="margin:0px;">
<%@ include file="top.jsp"%>
<table width="781" height="279" border="0" cellpadding="0" cellspacing="0" background="images/center.JPG"><tr><td>&nbsp;</td></tr>
</table>
<%@ include file="copyright.jsp"%>
</body>
</html>

bee11444da2a43afa753b20892adb5af.png

应用<jsp:include>标识包含网站Banner和版权信息栏

<%@ page language="java" contentType="text/html; charset=GB18030"pageEncoding="GB18030"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>使用文件包含include指令</title>
</head>
<body style="margin:0px;">
<%@ include file="top.jsp"%>
<table width="781" height="279" border="0" cellpadding="0" cellspacing="0" background="images/center.JPG"><tr><td>&nbsp;</td></tr>
</table>
<%@ jsp:include page="copyright.jsp"%>
</body>
</html>

应用<jsp:forward>标识将页面转发到用户登录页面

<%@ page language="java" contentType="text/html; charset=GB18030"pageEncoding="GB18030"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>中转页</title>
</head>
<body>
<jsp:forward page="login.jsp"/>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=GB18030"pageEncoding="GB18030"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>用户登录</title>
</head>
<body>
<form name="form1" method="post" action="">
用户名: <input	name="name" type="text" id="name" style="width: 120px"><br>
密&nbsp;&nbsp;码: <input name="pwd" type="password" id="pwd" style="width: 120px"> <br>
<br>
<input type="submit" name="Submit" value="提交">
</form>
</body>
</html>

8a8eb1f8b1164eb2955b2044067b0cf7.png

JSP内置对象

使用request对象获取请求参数值

<%@ page language="java" contentType="text/html; charset=GB18030"pageEncoding="GB18030"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>使用request对象获取请求参数值</title>
</head>
<body>
<a href="deal.jsp?id=1&user=">处理页</a>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=GB18030"pageEncoding="GB18030"%>
<%String id=request.getParameter("id");String user=request.getParameter("user");String pwd=request.getParameter("pwd");
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>处理页</title>
</head>
<body>
id 参数的值为:<%=id %><br>
user 参数的值为:<%=user %><br>
pwd 参数的值为:<%=pwd %>
</body>
</html>

43d671124cc94387bf73fc8320766415.png

使用request对象的色图Attribute()方法保存request范围内的变量,并应用request对象的getAttribute()方法读取request范围内的变量

<%@ page language="java" contentType="text/html; charset=GB18030"pageEncoding="GB18030"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body>
<%
try{//捕获异常信息int money=100;int number=0;request.setAttribute("result",money/number);//保存执行结果
}catch(Exception e){request.setAttribute("result","很抱歉,页面产生错误!");//保存错误提示信息
}
%>
<jsp:forward page="deal.jsp"/>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=GB18030"pageEncoding="GB18030"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>处理页</title>
</head>
<body>
<%String message=request.getAttribute("result").toString(); %>
<%=message %>
</body>
</html>

78800d88d6ea469cb581bb788861dd4d.png

通过cookie保存并读取用户登录信息

 

<%@ page language="java" contentType="text/html; charset=utf-8"pageEncoding="utf-8"%>
<%@ page import="java.net.URLDecoder" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>通过cookie保存并读取用户登录信息</title>
</head>
<body>
<%Cookie[ ]   cookies = request.getCookies();//从request中获得Cookie对象的集合String user = "";	//登录用户String date = "";	//注册的时间if (cookies != null) {for (int i = 0; i < cookies.length; i++) {	//遍历cookie对象的集合if (cookies[i].getName().equals("mrCookie")) {//如果cookie对象的名称为mrCookieuser = URLDecoder.decode(cookies[i].getValue().split("#")[0]);//获取用户名date = cookies[i].getValue().split("#")[1];//获取注册时间}}}if ("".equals(user) && "".equals(date)) {//如果没有注册
%>游客您好,欢迎您初次光临!<form action="deal.jsp" method="post">请输入姓名:<input name="user" type="text" value=""><input type="submit" value="确定"></form>
<%} else {//已经注册
%>欢迎[<b><%=user %></b>]再次光临<br>您注册的时间是:<%=date %>
<%}
%>
</body>
</html>
<%@page import="java.util.Date"%>
<%@ page language="java" contentType="text/html; charset=utf-8"pageEncoding="utf-8"%>
<%@ page import="java.net.URLEncoder" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>写入cookie</title>
</head>
<body>
<%request.setCharacterEncoding("utf-8");String user = URLEncoder.encode(request.getParameter("user"), "utf-8"); //获取用户名Cookie cookie = new Cookie("mrCookie", user+"#"+new Date().toLocaleString());cookie.setMaxAge(60 * 60 * 24 * 30); //设置cookie有效期30天response.addCookie(cookie); //保存cookie%>
<script type="text/javascript">window.location.href = "index.jsp"
</script>
</body>
</html>

2826a1674ac04bbd81eab1f692fc44ee.png

977439611dc247618621e93d56f6e0c6.png

使用request对象的相关方法获取客户端信息

<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>使用request对象的相关方法获取客户端信息</title>
</head>
<body>
<br>客户提交信息的方式:<%=request.getMethod()%>
<br>使用的协议:<%=request.getProtocol()%>
<br>获取发出请求字符串的客户端地址:<%=request.getRequestURI()%>
<br>获取发出请求字符串的客户端地址:<%=request.getRequestURL()%>
<br>获取提交数据的客户端IP地址:<%=request.getRemoteAddr()%>
<br>获取服务器端口号:<%=request.getServerPort()%>
<br>获取服务器的名称:<%=request.getServerName()%>
<br>获取客户端的主机名:<%=request.getRemoteHost()%>
<br>获取客户端所请求的脚本文件的文件路径:<%=request.getServletPath()%>
<br>获得Http协议定义的文件头信息Host的值:<%=request.getHeader("host")%>
<br>获得Http协议定义的文件头信息User-Agent的值:<%=request.getHeader("user-agent")%>
<br>获得Http协议定义的文件头信息accept-language的值:<%=request.getHeader("accept-language")%>
<br>获得请求文件的绝对路径:<%=request.getRealPath("index.jsp")%>
</body>
</html>

通过sendRedirect()方法重定向页面到用户登录页面

<%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>首页</title>
</head>
<body>
<%response.sendRedirect("login.jsp"); %>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=GB18030"pageEncoding="GB18030"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>用户登录页面</title>
</head>
<body>
<form name="form1" method="post" action="">
用户名: <input	name="name" type="text" id="name" style="width: 120px"><br>
密&nbsp;&nbsp;码: <input name="pwd" type="password" id="pwd" style="width: 120px"> <br>
<br>
<input type="submit" name="Submit" value="提交">
</form>
</body>
</html>

c663a96256624bed84753770f5083270.png

在index.jsp页面中,提供用户输入用户名文本框;在session.jsp页面中,将用户输入的用户名保存在session对象中,用户在该页面中可以添加最喜欢去的地方,在result.jsp页面中,显示用户输入的用户名与最想去的地方

<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%
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 'index.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="css/style.css"></head><body><form id="form1" name="form1" method="post" action="session.jsp"><div align="center"><table width="23%" border="0"><tr><td width="36%"><div align="center">您的名字是:</div></td><td width="64%"><label><div align="center"><input type="text" name="name" /></div></label></td></tr><tr><td colspan="2"><label><div align="center"><input type="submit" name="Submit" value="提交" /></div></label></td></tr></table>
</div>
</form></body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%
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 'result.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="css/style.css"></head><body><div align="center"><%String name = (String)session.getAttribute("name");		//获取保存在session范围内的对象String solution = request.getParameter("address");		//获取用户输入的最想去的地方%>
<form id="form1" name="form1" method="post" action=""><table width="28%" border="0"><tr><td colspan="2"><div align="center"><strong>显示答案</strong></div>          </td></tr><tr><td width="49%"><div align="left">您的名字是:</div></td><td width="51%"><label><div align="left"><%=name%></div>		<!-- 将用户输入的用户名在页面中显示 --></label></td></tr><tr><td><label><div align="left">您最喜欢去的地方是:</div></label></td><td><div align="left"><%=solution%></div></td> <!-- 将用户输入的最想去的地方在页面中显示 --></tr></table>
</form><p>&nbsp;</p >
</div></body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%
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 'session.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="css/style.css"></head><body><%String name = request.getParameter("name");		//获取用户填写的用户名session.setAttribute("name",name);				//将用户名保存在session对象中%><div align="center"><form id="form1" name="form1" method="post" action="result.jsp"><table width="28%" border="0"><tr><td>您的名字是:</td><td><%=name%></td></tr><tr><td>您最喜欢去的地方是:</td><td><label><input type="text" name="address" /></label></td></tr><tr><td colspan="2"><label><div align="center"><input type="submit" name="Submit" value="提交" /></div></label></td></tr></table></form><p>&nbsp;</p >
</div></body>
</html>

1650cabf0a974219ba94bbe520d2e2fe.png

在page指令中指定errorPage属性值为error.jsp,即指定显示异常信息页面,然后定义保存单价的request范围内的变量,并赋值为非数值型,最后获取变量并转换为float型

<%@ page language="java" contentType="text/html; charset=UTF-8" 
pageEncoding="UTF-8" errorPage="error.jsp"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>使用exception对象获取异常信息</title>
</head>
<body>
<%
request.setAttribute("price","12.5元");//保存单价到request范围内的变量price中
float price=Float.parseFloat(request.getAttribute("price").toString());	//获取单价,并转换为float型
%>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8" isErrorPage="true"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>错误提示页</title>
</head>
<body>
错误提示为:<%=exception.getMessage() %>
</body>
</html>

bc01f95ca05547c0b342758dbc5bca3b.png

JavaBean技术

通过非可视化的JavaBean,封装邮箱地址对象,通过JSP页面调用该对象来验证邮箱地址是否合法

package com.mingri;import java.io.Serializable;
/*** 邮件对象JavaBean* @author Li YongQiang*/
public class Email implements Serializable {//  serialVersionUID 值private static final long serialVersionUID = 1L;// Email地址private String mailAdd;// 是否是一个标准的Email地址private boolean eamil;/*** 默认无参的构造方法*/public Email() {}/*** 构造方法* @param mailAdd Email地址*/public Email(String mailAdd) {this.mailAdd = mailAdd;}/*** 是否是一个标准的Email地址* @return 布尔值*/public boolean isEamil() {// 正则表达式,定义邮箱格式String regex = "\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*"; // matches()方法可判断字符串是否与正则表达式匹配if (mailAdd.matches(regex)) { // eamil为真eamil = true;}// 返回eamilreturn eamil;}public String getMailAdd() {return mailAdd;}public void setMailAdd(String mailAdd) {this.mailAdd = mailAdd;}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body><form action="result.jsp" method="post"><table align="center" width="300" border="1" height="150"><tr><td colspan="2" align="center"><b>邮箱认证系统</b></td></tr><tr><td align="right">邮箱地址:</td><td><input type="text" name="mailAdd"/></td></tr><tr><td colspan="2" align="center"><input type="submit" /></td></tr></table></form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><%@page import="com.mingri.Email"%><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body><div align="center"><%// 获取邮箱地址String mailAdd = request.getParameter("mailAdd");// 实例化Email,并对mailAdd赋值Email email = new Email(mailAdd);// 判断是否是标准的邮箱地址if(email.isEamil()){out.print(mailAdd + " <br>是一个标准的邮箱地址!<br>");}else{out.print(mailAdd + " <br>不是一个标准的邮箱地址!<br>");}%>返回</div>
</body>
</html>

fc3eea7789e54857bcb13dfc8ed3ee71.png

92f1c32fa7b24d4a8ebb591b8f11ff33.png

28707ad8e05841909d4a1b0cde52545d.png

 

在JSP页面中显示JavaBean属性信息

package com.mingri;/*** 商品对象* @author Li YongQiang*/
public class Produce {// 商品名称private String name = "电吉他";// 商品价格private double price = 1880.5;// 数量private int count = 100;// 出厂地址private String factoryAdd = "吉林省长春市xxx琴行";public String getName() {return name;}public double getPrice() {return price;}public int getCount() {return count;}public String getFactoryAdd() {return factoryAdd;}
}
<%@ page language="java" contentType="text/html; charset=GB18030"pageEncoding="GB18030"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body><jsp:useBean id="produce" class="com.mingri.Produce"></jsp:useBean><div><ul><li>商品名称:<jsp:getProperty property="name" name="produce"/></li><li>价格:<jsp:getProperty property="price" name="produce"/></li><li>数量:<jsp:getProperty property="count" name="produce"/></li><li>厂址:<jsp:getProperty property="factoryAdd" name="produce"/></li></ul></div>
</body>
</html>

b8ab130aafbe48d097b40d7f43ce2ecb.png

在类中提供属性及与属性相对应的get()方法与set()方法,在JSP页面中对JavaBean属性赋值并获取输出

package com.mingri;public class Produce {// 商品名称private String name ;// 商品价格private double price ;// 数量private int count ;// 出厂地址private String factoryAdd ;public String getName() {return name;}public void setName(String name) {this.name = name;}public double getPrice() {return price;}public void setPrice(double price) {this.price = price;}public int getCount() {return count;}public void setCount(int count) {this.count = count;}public String getFactoryAdd() {return factoryAdd;}public void setFactoryAdd(String factoryAdd) {this.factoryAdd = factoryAdd;}
}
<%@ page language="java" contentType="text/html; charset=GB18030"pageEncoding="GB18030"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body><jsp:useBean id="produce" class="com.mingri.Produce"></jsp:useBean><jsp:setProperty property="name" name="produce" value="洗衣机"/><jsp:setProperty property="price" name="produce" value="8888.88"/><jsp:setProperty property="count" name="produce" value="88"/><jsp:setProperty property="factoryAdd" name="produce" value="广东省xxx公司"/><div><ul><li>商品名称:<jsp:getProperty property="name" name="produce"/></li><li>价格:<jsp:getProperty property="price" name="produce"/></li><li>数量:<jsp:getProperty property="count" name="produce"/></li><li>厂址:<jsp:getProperty property="factoryAdd" name="produce"/></li></ul></div>
</body>
</html>

a87fcb39f08747599ca7f6273cb90d94.png

实现档案管理系统,在其中录入用户信息功能,主要通过在JSP页面中应用JavaBean进行实现

package com.mingri;/*** 用户信息* @author Li YongQiang*/
public class Person {// 姓名private String name;// 年龄private int age;// 性别private String sex;// 住址private String add;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}public String getAdd() {return add;}public void setAdd(String add) {this.add = add;}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body><form action="reg.jsp" method="post"><table align="center" width="400" height="200" border="1"><tr><td align="center" colspan="2" height="40"><b>添加用户信息</b></td></tr><tr><td align="right">姓 名:</td><td><input type="text" name="name"></td></tr><tr><td align="right">年 龄:</td><td><input type="text" name="age"></td></tr><tr><td align="right">性 别:</td><td><input type="text" name="sex"></td></tr><tr><td align="right">住 址:</td><td><input type="text" name="add"></td></tr><tr><td align="center" colspan="2"><input type="submit" value="添 加"></td></tr></table></form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body><%request.setCharacterEncoding("UTF-8");%><jsp:useBean id="person" class="com.mingri.Person" scope="page"><jsp:setProperty name="person" property="*"/></jsp:useBean><table align="center" width="400"><tr><td align="right">姓 名:</td><td><jsp:getProperty property="name" name="person"/></td></tr><tr><td align="right">年 龄:</td><td><jsp:getProperty property="age" name="person"/></td></tr><tr><td align="right">性 别:</td><td><jsp:getProperty property="sex" name="person"/></td></tr><tr><td align="right">住 址:</td><td><jsp:getProperty property="add" name="person"/></td></tr></table>
</body>
</html>

d6df3cb49e2142d698d2ff459694b03b.png

cc1ed8d730784d3aa0ea6a454713b8b0.png

 

通过编写对字符转码的JavaBean,来解决在新闻发布会系统中,发布中文信息的乱码现象

package com.mingri;public class News {// 标题private String title;// 内容private String content;public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public String getContent() {return content;}public void setContent(String content) {this.content = content;}
}
package com.mingri;import java.io.UnsupportedEncodingException;public class CharactorEncoding {/*** 构造方法*/public CharactorEncoding(){}/*** 对字符进行转码处理* @param str 要转码的字符串* @return 编码后的字符串*/public String toString(String str){// 转换字符String text = "";// 判断要转码的字符串是否有效if(str != null && !"".equals(str)){try {// 将字符串进行编码处理text = new String(str.getBytes("iso8859-1"),"GB18030");} catch (UnsupportedEncodingException e) {e.printStackTrace();}}// 返回后的字符串return text;}
}
<%@ page language="java" contentType="text/html; charset=GB18030"pageEncoding="GB18030"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>XX新闻发布系统</title>
</head>
<body><form action="release.jsp" method="post"><table align="center" width="450" height="260" border="1"><tr><td align="center" colspan="2" height="40" ><b>新闻发布</b></td></tr><tr><td align="right">标 题:</td><td><input type="text" name="title" size="30"></td></tr><tr><td align="right">内 容:</td><td><textarea name="content" rows="8" cols="40"></textarea></td></tr><tr><td align="center" colspan="2"><input type="submit" value="发 布"></td></tr></table></form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=GB18030"pageEncoding="GB18030"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>发布结果</title>
<style type="text/css">#container{width: 450px;border: solid 1px;padding: 20px;}#title{font-size: 16px;font-weight: bold;color: #3399FF;}#content{font-size: 12px;text-align: left;}
</style>
</head>
<body><jsp:useBean id="news" class="com.mingri.News"></jsp:useBean><jsp:useBean id="encoding" class="com.mingri.CharactorEncoding"></jsp:useBean><jsp:setProperty property="*" name="news"/><div align="center"><div id="container"><div id="title"><%= encoding.toString(news.getTitle())%></div><hr><div id="content"><%= encoding.toString(news.getContent())%></div></div></div>
</body>
</html>

d28104ca67c9474198df4ce84d13682f.png

69f041c996624011b4a8603bc623068e.png

创建获取当前时间的JavaBean对象,该对象既可以获取当日期,同时也可以获取今天是星期几

package com.mingri;import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/*** 获取当前时间的JavaBean* @author Li YongQiang* */
public class DateBean {// 日期及时间private String dateTime;// 星期private String week;// Calendar对象private Calendar calendar = Calendar.getInstance();/*** 获取当前日期及时间* @return 日期及时间的字符串*/public String getDateTime() {// 获取当前时间Date currDate = Calendar.getInstance().getTime();// 实例化SimpleDateFormatSimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH点mm分ss秒");// 格式化日期时间dateTime = sdf.format(currDate);// 返回日期及时间的字符串return dateTime;}/*** 获取星期几* @return 返回星期字符串*/public String getWeek() {// 定义数组String[] weeks = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };// 获取一星期的某天int index = calendar.get(Calendar.DAY_OF_WEEK);// 获取星期几week = weeks[index - 1];// 返回星期字符串return week;}
}
<%@ page language="java" contentType="text/html; charset=GB18030"pageEncoding="GB18030"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>电子时钟</title>
<style type="text/css">#clock{width:420px;height:80px;background:#E0E0E0;font-size: 25px;font-weight: bold;border: solid 5px orange;padding: 20px;}#week{padding-top:15px;color: #0080FF;}
</style>
<meta http-equiv="Refresh" content="1">
</head>
<body><jsp:useBean id="date" class="com.mingri.DateBean" scope="application"></jsp:useBean><div align="center"><div id="clock"><div id="time"><jsp:getProperty property="dateTime" name="date"/></div><div id="week"><jsp:getProperty property="week" name="date"/></div></div></div>
</body>
</html>

f73a9ff3ae2e46e7858a403baf3218e1.png

 

创建将字符串转换成数组的JavaBean,实现对“问卷调查”表单中复选框的数值的处理

package com.mingri;import java.io.Serializable;public class Paper implements Serializable {private static final long serialVersionUID = 1L;// 编程语言private String[] languages;// 掌握技术private String[] technics;// 部分private String[] parts;public Paper(){}public String[] getLanguages() {return languages;}public void setLanguages(String[] languages) {this.languages = languages;}public String[] getTechnics() {return technics;}public void setTechnics(String[] technics) {this.technics = technics;}public String[] getParts() {return parts;}public void setParts(String[] parts) {this.parts = parts;}
}
package com.mingri;public class Convert {/*** 将数组转换成为字符串* @param arr 数组* @return 字符串*/public String arr2Str(String[] arr){// 实例化StringBufferStringBuffer sb = new StringBuffer();// 判断arr是否为有效数组if(arr != null && arr.length > 0){// 遍历数组for (String s : arr) {// 将字符串追加到StringBuffer中sb.append(s);// 将字符串追加到StringBuffer中sb.append(",");}// 判断字符串长度是否有效if(sb.length() > 0){// 截取字符sb = sb.deleteCharAt(sb.length() - 1);}}// 返回字符串return sb.toString();}
}
<%@ page language="java" contentType="text/html; charset=GB18030"pageEncoding="GB18030"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>调查问卷</title>
</head><body><form action="reg.jsp" method="post"><div><h1>调查问卷</h1><hr/><ul><li>你经常用哪些编程语言开发程序:</li><li><input type="checkbox" name="languages" value="JAVA">JAVA<input type="checkbox" name="languages" value="PHP">PHP<input type="checkbox" name="languages" value=".NET">.NET<input type="checkbox" name="languages" value="VC++">VC++</li></ul><ul><li>你目前所掌握的技术:</li><li><input type="checkbox" name="technics" value="HTML">HTML<input type="checkbox" name="technics" value="JAVA BEAN">JAVA BEAN<input type="checkbox" name="technics" value="JSP">JSP<input type="checkbox" name="technics" value="SERVLET">SERVLET</li></ul><ul><li>在学习中哪一部分感觉有困难:</li><li><input type="checkbox" name="parts" value="JSP">JSP<input type="checkbox" name="parts" value="STRUTS">STRUTS</li></ul>&nbsp;&nbsp;&nbsp;&nbsp;<input type="submit" value="提 交"></div></form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=GB18030"pageEncoding="GB18030"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>调查结果</title>
</head><body><jsp:useBean id="paper" class="com.mingri.Paper"></jsp:useBean><jsp:useBean id="convert" class="com.mingri.Convert"></jsp:useBean><jsp:setProperty property="*" name="paper"/><div><h1>调查结果</h1><hr/><ul><li>你经常使用的编程语言:<%= convert.arr2Str(paper.getLanguages()) %> 。</li><li>你目前所掌握的技术:<%= convert.arr2Str(paper.getTechnics()) %> 。</li><li>在学习中感觉有困难的部分:<%= convert.arr2Str(paper.getParts()) %> 。</li></ul></div>
</body>
</html>

282522ea97c54a1b926c3c5fde1e7467.png

93a7ca88c28c4c02a3587838d264da72.png

Servlet技术

创建一个名称为TestServlet的Servlet

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class TestServlet extends HttpServlet {//初始化方法public void init() throws ServletException {}//处理HTTP Get请求public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {}//处理HTTP Post请求public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {}//处理HTTP Put请求public void doPut(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {}//处理HTTP Delete请求public void doDelete(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {}//销毁方法public void destroy() {super.destroy();}
}

创建一个Servlet,实现向客户端输出一个字符串

public class WordServlet implements Servlet {public void destroy() {// TODO Auto-generated method stub}public ServletConfig getServletConfig() {// TODO Auto-generated method stubreturn null;}public String getServletInfo() {// TODO Auto-generated method stubreturn null;}public void init(ServletConfig arg0) throws ServletException {// TODO Auto-generated method stub}public void service(ServletRequest request, ServletResponse response)throws ServletException, IOException {PrintWriter pwt = response.getWriter();pwt.println("mingrisoft");pwt.close();}
}

创建一个简易的Servlet计算器

package com.mingri;import java.io.IOException;
import java.io.PrintWriter;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;public class CalculateServlet extends HttpServlet {private static final long serialVersionUID = 7223778025721767631L;@Overridepublic void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setContentType("text/html");response.setCharacterEncoding("GBK");PrintWriter out = response.getWriter();// 获取第一个数字double firstNum = Double.valueOf(request.getParameter("firstNum"));// 获取第一个数字double secondNum = Double.valueOf(request.getParameter("secendNum"));// 获取运算符String operator = request.getParameter("operator");// 计算结果double result = 0;// 判断运算符if("+".equals(operator)){result = firstNum + secondNum;}else if("-".equals(operator)){result = firstNum - secondNum;}else if("*".equals(operator)){result = firstNum * secondNum;}else if("/".equals(operator)){result = firstNum / secondNum;}// 输出计算结果out.print(firstNum + " " + operator +  " " + secondNum + " = " + result);out.print("<br>返回");out.flush();out.close();}}
<%@ page language="java" contentType="text/html" pageEncoding="GBK"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><title>简易Servlet计算器</title><!--<link rel="stylesheet" type="text/css" href="styles.css">--><script type="text/javascript">function calc(form){with(form){if(firstNum.value == "" || secendNum.value == ""){alert("请输入数字!");return false;}if(isNaN(firstNum.value) || isNaN(secendNum.value)){alert("数字格式错误!");return false;}if(operator.value == "-1"){alert("请选择运算符!");return false;}}}</script></head><body><form action="CalculateServlet" method="post" onsubmit="return calc(this);"><table align="center" border="0"><tr><th>简易Servlet计算器</th></tr><tr><td><input type="text" name="firstNum"><select name="operator"><option value="-1">运算符</option><option value="+">+</option><option value="-">-</option><option value="*">*</option><option value="/">/</option></select><input type="text" name="secendNum"><input type="submit" value="计算"></td></tr></table></form></body>
</html>
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"><servlet><servlet-name>CalculateServlet</servlet-name><servlet-class>com.mingri.CalculateServlet</servlet-class></servlet><servlet-mapping><servlet-name>CalculateServlet</servlet-name><url-pattern>/CalculateServlet</url-pattern></servlet-mapping><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list>
</web-app>

1570b1ef2bd544858dd5067a76e80c5a.png

367a5bdb29884e73a15d83a88384b0b2.png

过滤器和监听器

创建一个过滤器,实现网站访问计数器的功能,并在web.xml文件的配置中,将网站访问量的初始值设置5000

package com.mingri;import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;public class CountFilter implements Filter {// 来访数量private int count;@Overridepublic void init(FilterConfig filterConfig) throws ServletException {// 获取初始化参数String param = filterConfig.getInitParameter("count");// 将字符串转换为intcount = Integer.valueOf(param);}@Overridepublic void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException {// 访问数量自增count ++;// 将ServletRequest转换成HttpServletRequestHttpServletRequest req = (HttpServletRequest) request;// 获取ServletContextServletContext context = req.getSession().getServletContext();// 将来访数量值放入到ServletContext中context.setAttribute("count", count);// 向下传递过滤器chain.doFilter(request, response);}@Overridepublic void destroy() {}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>人数统计</title>
</head>
<body><h2>欢迎光临,<br>您是本站的第【 <%=application.getAttribute("count") %>】位访客!</h2>
</body>
</html>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"id="WebApp_ID" version="2.5"><display-name>10.3</display-name><welcome-file-list><welcome-file>index.html</welcome-file><welcome-file>index.htm</welcome-file><welcome-file>index.jsp</welcome-file><welcome-file>default.html</welcome-file><welcome-file>default.htm</welcome-file><welcome-file>default.jsp</welcome-file></welcome-file-list><!-- 过滤器声明 --><filter><!-- 过滤器的名称 --><filter-name>CountFilter</filter-name><!-- 过滤器的完整类名 --><filter-class>com.mingri.CountFilter</filter-class><!-- 设置初始化参数 --><init-param><!-- 参数名 --><param-name>count</param-name><!-- 参数值 --><param-value>5000</param-value></init-param></filter><!-- 过滤器映射 --><filter-mapping><!-- 过滤器名称 --><filter-name>CountFilter</filter-name><!-- 过滤器URL映射 --><url-pattern>/index.jsp</url-pattern></filter-mapping>
</web-app>

49ad096e96474046974f3e1fd811b234.png

实现图书信息的添加功能,并创建字符编码过滤器,避免中文乱码现象的产生

package com.mingri;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 CharactorFilter implements Filter {// 字符编码String encoding = null;@Overridepublic void destroy() {encoding = null;}@Overridepublic void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException {// 判断字符编码是否为空if(encoding != null){// 设置request的编码格式request.setCharacterEncoding(encoding);// 设置response字符编码response.setContentType("text/html; charset="+encoding);}// 传递给下一过滤器chain.doFilter(request, response);}@Overridepublic void init(FilterConfig filterConfig) throws ServletException {// 获取初始化参数encoding = filterConfig.getInitParameter("encoding");}}
package com.mingri;import java.io.IOException;
import java.io.PrintWriter;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;/*** 添加图书信息的Servlet*/
public class AddServlet extends HttpServlet {private static final long serialVersionUID = 1L;// 处理GET请求protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doPost(request, response);}// 处理POST请求protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// 获取 PrintWriterPrintWriter out = response.getWriter();// 获取图书编号String id = request.getParameter("id");// 获取名称String name = request.getParameter("name");// 获取作者String author = request.getParameter("author");// 获取价格String price = request.getParameter("price");// 输出图书信息out.print("<h2>图书信息添加成功</h2><hr>");out.print("图书编号:" + id + "<br>");out.print("图书名称:" + name + "<br>");out.print("作者:" + author + "<br>");out.print("价格:" + price + "<br>");// 刷新流out.flush();// 关闭流out.close();}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>添加图书信息</title>
</head>
<body><form action="AddServlet" method="post"><table align="center" border="1" width="350"><tr><td class="2" align="center" colspan="2"><h2>添加图书信息</h2></td></tr><tr><td align="right">图书编号:</td><td><input type="text" name="id"></td></tr><tr><td align="right">图书名称:</td><td><input type="text" name="name"></td></tr><tr><td align="right">作  者:</td><td><input type="text" name="author"></td></tr><tr><td align="right">价  格:</td><td><input type="text" name="price"></td></tr><tr><td class="2" align="center" colspan="2"><input type="submit" value="添 加"></td></tr></table></form>
</body>
</html>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"id="WebApp_ID" version="2.5"><display-name>10.3</display-name><welcome-file-list><welcome-file>index.html</welcome-file><welcome-file>index.htm</welcome-file><welcome-file>index.jsp</welcome-file><welcome-file>default.html</welcome-file><welcome-file>default.htm</welcome-file><welcome-file>default.jsp</welcome-file></welcome-file-list><!-- 声明过滤器 -->
<filter><!-- 过滤器名称 --><filter-name>CharactorFilter</filter-name><!-- 过滤器的完整类名 --><filter-class>com.mingri.CharactorFilter</filter-class><!-- 初始化参数 --><init-param><!-- 参数名 --><param-name>encoding</param-name><!-- 参数值 --><param-value>UTF-8</param-value></init-param>
</filter>
<!-- 过滤器映射 -->
<filter-mapping><!-- 过滤器名称 --><filter-name>CharactorFilter</filter-name><!-- URL映射 --><url-pattern>/*</url-pattern>
</filter-mapping><!-- 声明Servlet --><servlet><!-- Servlet名称 --><servlet-name>AddServlet</servlet-name><!-- Servlet完整类名 --><servlet-class>com.mingri.AddServlet</servlet-class></servlet><!-- Servlet映射 --><servlet-mapping><!-- Servlet名称 --><servlet-name>AddServlet</servlet-name><!-- URL映射 --><url-pattern>/AddServlet</url-pattern></servlet-mapping>
</web-app>

a7030e2bcdfa4239a8b5670e346cbe12.png

4ea5aea541e544e5869682041f773e29.png

 

应用Servlet监听器统计在线人数

<%@ page contentType="text/html; charset=UTF-8" language="java" import="java.sql.*" errorPage="" %>
<%@ page import="java.util.*"%>
<%@ page import="com.mingri.*"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>使用监听查看在线用户</title>
<link href="css/style.css" rel="stylesheet" type="text/css">
</head>
<%
UserInfoList list=UserInfoList.getInstance();
UserInfoTrace ut=new UserInfoTrace();
String name=request.getParameter("user");
ut.setUser(name);
session.setAttribute("list",ut);
list.addUserInfo(ut.getUser());
session.setMaxInactiveInterval(10);
%>
<body>
<div align="center"><table width="506" height="246" border="0" cellpadding="0" cellspacing="0" background="image/background2.jpg"><tr><td align="center"><br><textarea rows="8" cols="20">
<%
Vector vector=list.getList();
if(vector!=null&&vector.size()>0){
for(int i=0;i<vector.size();i++){out.println(vector.elementAt(i));
}
}
%>
</textarea><br><br>返回</td></tr>
</table>
</div>
</body>
</html>
package com.mingri;import java.util.*;public class UserInfoList {private static UserInfoList user = new UserInfoList();private Vector vector = null;/*利用private调用构造函数,防止被外界产生新的instance对象*/public UserInfoList() {this.vector = new Vector();}/*外界使用的instance对象*/public static UserInfoList getInstance() {return user;}/*增加用户*/public boolean addUserInfo(String user) {if (user != null) {this.vector.add(user);return true;} else {return false;}}/*获取用户列表*/public Vector getList() {return vector;}/*移除用户*/public void removeUserInfo(String user) {if (user != null) {vector.removeElement(user);}}}
package com.mingri;import javax.servlet.http.HttpSessionBindingEvent;public class UserInfoTrace implements javax.servlet.http.HttpSessionBindingListener {private String user;private UserInfoList container = UserInfoList.getInstance();public UserInfoTrace() {user = "";}public void setUser(String user) {this.user = user;}public String getUser() {return this.user;}public void valueBound(HttpSessionBindingEvent arg0) {System.out.println("上线" + this.user);}public void valueUnbound(HttpSessionBindingEvent arg0) {System.out.println("下线" + this.user);if (user != "") {container.removeUserInfo(user);}}}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/723567.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

马蹄集 开学季活动赛

目录 最大的平均值 门票 二阶前缀和 最大异或和 背军理 信号灯 切蛋糕 矩形 矩阵01变 运动会进行中 三连 距离平方和 最大的平均值 黄金时间限制:1秒巴 占用内存:128 M少难度: 给一个数组&#xff0c;长度为n&#xff0c;找一个长度大于等于m的子区间&#xff0c;使…

【Redis】redis的基本使用

&#x1f4dd;个人主页&#xff1a;五敷有你 &#x1f525;系列专栏&#xff1a;Redis ⛺️稳中求进&#xff0c;晒太阳 Redis的概述 为什么要有redis? redis是数据库&#xff0c;mysql也是数据库&#xff0c;redis做缓存的意义就是为了减轻数据库压力 数据库为什么…

微信公众号公司主体变更怎么办?

公众号迁移的好处有哪些&#xff1f;迁移后原公众号还能用吗&#xff1f;1&#xff09;获得更多权限功能如果公众号是个人主体&#xff0c;想进行认证&#xff0c;拥有更多权限功能。例如菜单栏跳转外部链接&#xff0c;相拥有留言功能&#xff0c;服务号认证获得开发权限等。就…

平台总线式驱动开发

一、总线、设备、驱动 硬编码式的驱动开发带来的问题&#xff1a; 垃圾代码太多 结构不清晰 一些统一设备功能难以支持 开发效率低下 1.1 初期解决思路&#xff1a;设备和驱动分离 struct device来表示一个具体设备&#xff0c;主要提供具体设备相关的资源&#xff08;如…

openEuler系统远程SSH远程连接

文章目录 1. 本地SSH连接测试2. openEuler安装Cpolar3. 配置 SSH公网地址4. 公网远程SSH连接5. 固定连接SSH公网地址6. SSH固定地址连接测试 欧拉操作系统(openEuler, 简称“欧拉”)是面向数字基础设施的操作系统,支持服务器、云计算、边缘openEuler是面向数字基础设施的操作系…

基于51单片机的心率体温检测系统设计

目 录 摘 要 I Abstract II 引 言 1 1 控制系统设计 2 1.1 主控系统方案设计 2 1.2 脉搏传感器方案设计 3 1.3 系统工作原理 5 2 硬件设计 6 2.1 主电路 6 2.1.1 单片机的选择 6 2.1.2 STC89C51的主要功能及性能参数 6 2.1.3 STC89C51单片机引脚说明 6 2.2 驱动电路 8 2.2.1 比…

第八篇 - 预测受众(Predictive audience)技术是如何赋能数字化营销生态的?- 我为什么要翻译介绍美国人工智能科技巨头IAB公司

IAB平台&#xff0c;使命和功能 IAB成立于1996年&#xff0c;总部位于纽约市。 作为美国的人工智能科技巨头社会媒体和营销专业平台公司&#xff0c;互动广告局&#xff08;IAB- the Interactive Advertising Bureau&#xff09;自1996年成立以来&#xff0c;先后为700多家媒…

基于springboot+vue的政府管理系统

博主主页&#xff1a;猫头鹰源码 博主简介&#xff1a;Java领域优质创作者、CSDN博客专家、阿里云专家博主、公司架构师、全网粉丝5万、专注Java技术领域和毕业设计项目实战&#xff0c;欢迎高校老师\讲师\同行交流合作 ​主要内容&#xff1a;毕业设计(Javaweb项目|小程序|Pyt…

2024BAT大厂Android社招面试题,OMG,学它

开头 中国互联网发展的这些年&#xff0c;如今90后程序员是中国程序员的主力军&#xff0c;互联网的热潮也让一批批00后蠢蠢欲动&#xff0c;尝试涌入互联网圈。 当程序员容易&#xff0c;当一个优秀的程序员需要不断学习&#xff0c;从初级程序员到高级程序员&#xff0c;从…

【NR 定位】3GPP NR Positioning 5G定位标准解读(三)

目录 前言 5 NG-RAN UE定位架构 5.1 架构 5.2 UE定位操作 5.3 NG-RAN定位操作 5.3.1 通用NG-RAN定位操作 5.3.2 OTDOA定位支持 5.3.3 广播辅助信息支持 5.3.4 NR RAT相关定位支持 5.4 NG-RAN中与UE定位相关的元素功能描述 5.4.1 用户设备&#xff08;UE&#xff09; …

蓝桥杯嵌入式省赛模板构建——测量两路频率和占空比

结合测量一路PWM频率编程 测量占空比&#xff1a;测量高电平持续时间和周期 思路&#xff1a; ①.第一次上升沿中断&#xff0c;清零计数器&#xff0c;开始计时并改成下降沿中断 ②.下降沿中断&#xff0c;获取计数值T1&#xff0c;并改为上升沿中断 ③.第二次上升沿中断…

小猫挑食不吃猫粮是为什么?排行榜靠前适口性好的生骨肉冻干分享

现在的猫咪主人都把自家的小猫当成了心头的宝贝&#xff0c;呵护备至。但宠爱过度有时会导致猫咪形成挑食的坏习惯。小猫挑食不吃猫粮是为什么呢&#xff1f;面对这一问题&#xff0c;我们应该如何解决呢&#xff1f; 今天&#xff0c;我给大家剖析一下小猫挑食不吃猫粮是为什…

青少年软件编程图形化编程等级考试二级模拟测试

青少年软件编程图形化编程等级考试二级模拟测试 第 1 题 【单选题】 下面积木块的执行结果是&#xff1f;&#xff08; &#xff09; A :香蕉苹果 B :香蕉和苹果 C :香苹 D :香果 正确答案:D 试题解析: 第 2 题 【单选题】 执行下面的指令后&#xff0c; 舞台上的小…

ActivityRecordInputSink深入剖析全网独家分析

背景&#xff1a; 学员粉丝朋友们在学习马哥的android framework实战高级课程时候&#xff0c;有一些细心的学员提出了一个无问题&#xff0c;那就是再看dumpsys SurfaceFlinger的图层或者是dumpsys input相关信息时候发现了一个叫做ActivityRecordInputSink的图层。 如下图所…

OWASP Top 10 网络安全10大漏洞——A02:A02:2021-加密机制失效

10大Web应用程序安全风险 2021年top10中有三个新类别、四个类别的命名和范围变化&#xff0c;以及一些合并。 A02&#xff1a;A02:2021-加密机制失效 上升一个位置&#xff0c;当前top2&#xff0c;以前称为敏感数据泄露&#xff0c;是一种状况而不是根本原因。更新后的类别…

MacOS开发环境搭建

MacOS开发环境搭建 一、MacOS二、Python三、MacOS搭建Python开发环境1.Python下载地址1.1 Python官网地址1.2 Python下载地址 2.安装Python3.安装Python4.安装PyCharm5.创建一个Python项目6.配置PyCharm7.安装Python包8.运行Python代码9.总结 一、MacOS macOS是一套由苹果开发的…

2024京东Java面试真题解析,每次面试必问的二叉树的设计与编码

一、背景 我们日常在电商网站购物时经常会遇到一些高并发的场景&#xff0c;例如电商 App 上经常出现的秒杀活动、限量优惠券抢购&#xff0c;还有我们去哪儿网的火车票抢票系统等&#xff0c;这些场景有一个共同特点就是访问量激增&#xff0c;虽然在系统设计时会通过限流、异…

前面说什么是前后端分类,那到底是怎么个分类法呢?

前后端分离是指将一个web 系统的动态内容和静态内容进行分离&#xff0c;包括其开发、部署等。 比如传统的 MVC 架构&#xff0c;HTML、JS、CSS… 等前端代码和 Java、spring、mybatis… 等后端代码是在同一个项目中进行开发、部署的。那前后端分离后&#xff0c;就可以分多个项…

学习vue3使用在线官方开发环境play.vuejs.org进行测试

更多ruoyi-nbcio功能请看演示系统 gitee源代码地址 前后端代码&#xff1a; https://gitee.com/nbacheng/ruoyi-nbcio 演示地址&#xff1a; http://122.227.135.243:9666 更多nbcio-boot功能请看演示系统 gitee源代码地址 后端代码&#xff1a; https://gitee.com/nbach…

C++ STL自定义排序

更具体的看【速记】C STL自定义排序 - 知乎 (zhihu.com) sort sort第三个位置放的greater<int>和less<int>萌新可能会弄错&#xff0c;这两个单词不是更大和更小的意思&#xff0c;而是大于和小于&#xff0c;并且比较就是自定义排序中的前者和后者。 如果是less…