自定义Tomcat服务器
public static void main(String[] args) {
try {
ServerSocket serverSocket = new ServerSocket(8282);
while (true) {
//阻塞
//没有请求时,阻塞这个代码,则不创建线程
Socket socket = serverSocket.accept();
new Thread(){
@Override
public void run() {
try {
//处理请求行,获取访问资源的路径
InputStream inputStream = socket.getInputStream();
//转换流
InputStreamReader reader = new InputStreamReader(inputStream);
//封装成了高效字符流
BufferedReader bufferedReader = new BufferedReader(reader);
//请求方式、请求路径、协议;其中,只有请求路径有用
String line = bufferedReader.readLine();
String[] requestInfos = line.split(" ");
String requestURL = requestInfos[1];
System.out.println(requestURL);
int length = "/day67/".length();
//获取请求资源的相对路径
requestURL = requestURL.substring(length);
//通过服务器将index.html响应给浏览器 , 通过socket.getOutputStream();
OutputStream outputStream = socket.getOutputStream();
BufferedOutputStream bos = new BufferedOutputStream(outputStream);
//操作响应行 (协议、响应状态码)
bos.write("HTTP/1.1 200 OK\r\n".getBytes());
//操作响应头(Content-Type)
bos.write("Content-Type:text/html\r\n".getBytes());
bos.write("\r\n".getBytes());
//操作响应正文
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(requestURL));
int len = -1;
byte[] bys = new byte[8192];
while ((len = bis.read(bys)) != -1) {
bos.write(bys,0,len);
}
bis.close();
bos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
}
} catch (Exception e) {
e.printStackTrace();
}
}
正则表达式案例
创建一个User对象,有三个字段:账户、密码、邮箱,有如下要求:
1.账户: 由数字和字母组成,第一个位置上必须是大写字母,长度范围为6~10 [A-Z]{1}[a-zA-Z0-9]{5,9}
2.密码: 由数字、字母和_组成,第一位置上必须是字母,长度为6 [a-zA-Z]{1}\w{5}
3.邮箱: 由字母、@和.组成,长度范围为8~10,必须是qq邮箱 [a-zA-Z]{1,3}@qq.com
public class MyIllegalArgumentsException extends RuntimeException{
public MyIllegalArgumentsException() {
}
public MyIllegalArgumentsException(String message) {
super(message);
}
public MyIllegalArgumentsException(String message, Throwable cause) {
super(message, cause);
}
public MyIllegalArgumentsException(Throwable cause) {
super(cause);
}
public MyIllegalArgumentsException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
import com.qfedu.exception.MyIllegalArgumentsException;
public class User {
private String username;//由数字和字母组成,第一个位置上必须是大写字母,长度范围为6~10
private String password;//由数字、字母和_组成,第一位置上必须是字母,长度为6
private String email;//由字母、@和.组成,长度范围为8~10,必须是qq邮箱
public User() {
}
public User(String username, String password, String email) {
if (validateUsername(username)) {
this.username = username;
} else {
throw new MyIllegalArgumentsException("账户错误,由数字和字母组成,第一个位置上必须是大写字母,长度范围为6~10");
}
if (validatePassword(password)) {
this.password = password;
} else {
throw new MyIllegalArgumentsException("密码错误,由数字、字母和_组成,第一位置上必须是字母,长度为6");
}
if (validateEmail(email)) {
this.email = email;
} else {
throw new MyIllegalArgumentsException("邮箱错误,由字母、@和.组成,长度范围为8~10,必须是qq邮箱");
}
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
//设置用户名之前校验
boolean flag = validateUsername(username);
if (flag) {
this.username = username;
} else {
throw new MyIllegalArgumentsException("账户错误,由数字和字母组成,第一个位置上必须是大写字母,长度范围为6~10");
}
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
boolean flag = validatePassword(password);
if (flag) {
this.password = password;
} else {
throw new MyIllegalArgumentsException("密码错误,由数字、字母和_组成,第一位置上必须是字母,长度为6");
}
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
if (validateEmail(email)) {
this.email = email;
} else {
throw new MyIllegalArgumentsException("邮箱错误,由字母、@和.组成,长度范围为8~10,必须是qq邮箱");
}
}
@Override
public String toString() {
return "User{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
", email='" + email + '\'' +
'}';
}
/**
* 校验用户名
*/
private boolean validateUsername(String username){
String reg = "[A-Z]{1}[a-zA-Z0-9]{5,9}";
boolean matches = username.matches(reg);
return matches;
}
/**
* 校验密码
*/
private boolean validatePassword(String password){
String reg = "[a-zA-Z]{1}\\w{5}";
boolean matches = password.matches(reg);
return matches;
}
/**
* 校验邮箱
*/
private boolean validateEmail(String email){
String reg = "[a-zA-Z]{1,3}@qq\\.com";
boolean matches = email.matches(reg);
return matches;
}
}
User user = new User();
user.setUsername("zhangsan");
user.setPassword("root1234");
user.setEmail("[email protected]");
System.out.println(user);
JavaScript
注:Js脚本放置的位置会决定它的执行顺序
在Js中,switch语句可以接受任意的原始数据类型
Jackson
//将java对象转换为json字符串
Province p1 = new Province(1,"咖啡");
List cityList1 = new ArrayList<>();
cityList1.add(new City(11,"单品咖啡"));
cityList1.add(new City(12,"速溶咖啡"));
cityList1.add(new City(13,"混合咖啡"));
p1.setCityList(cityList1);
//p1 -> json字符串
ObjectMapper objectMapper = new ObjectMapper();
String jsonStr1 = objectMapper.writeValueAsString(p1);
System.out.println(jsonStr1);
Province p2 = new Province(2,"茶叶");
List cityList2 = new ArrayList<>();
cityList2.add(new City(21,"青茶"));
cityList2.add(new City(22,"绿茶"));
cityList2.add(new City(23,"红茶"));
p2.setCityList(cityList2);
//provinceList -> json字符串
List provinceList = new ArrayList<>();
provinceList.add(p1);
provinceList.add(p2);
String jsonStr2 = objectMapper.writeValueAsString(provinceList);
System.out.println(jsonStr2);
//map -> json字符串
Map map = new HashMap<>();
map.put("p1",p1);
map.put("p2",p2);
String jsonStr3 = objectMapper.writeValueAsString(map);
System.out.println(jsonStr3);
AJAX入门案例
function createXMLHttpRequest(){
var xmlHttp;
try{ // Firefox, Opera 8.0+, Safari,Google Chrome
xmlHttp=new XMLHttpRequest();
} catch (e){
try{// Internet Explorer
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}catch (e){
try{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}catch (e){}
}
}
return xmlHttp;
}
function ajax_get(){
// * 1.创建异步的XMLHttpRequest对象.
var xhr = createXMLHttpRequest();
// * 2.设置监听:监听对象的状态的变化,触发一个函数.
xhr.onreadystatechange = function(){
if(xhr.readyState == 4){
if(xhr.status == 200){
var data = xhr.responseText;
// 获得div:
var div1 = document.getElementById("div1");
div1.innerHTML = data;
}
}
}
// * 3.打开链接:
xhr.open("GET","${pageContext.request.contextPath}/ajaxServletDemo1",true);
// * 4.发送数据:
xhr.send();
}
function ajax_post() {
// * 1.创建异步的XMLHttpRequest对象.
var xhr = createXMLHttpRequest();
// * 2.设置监听:监听对象的状态的变化,触发一个函数.
xhr.onreadystatechange = function(){
if(xhr.readyState == 4){
if(xhr.status == 200){
// 获得响应的数据:
var data = xhr.responseText;
// 获得div1:
var div1 = document.getElementById("div1");
div1.innerHTML = data;
}
}
}
// * 3.打开链接:
xhr.open("POST","${ pageContext.request.contextPath }/ajaxServletDemo1",true);
// 设置请求头:
xhr.setRequestHeader("Content‐Type","application/x‐www‐form‐urlencoded");
// * 4.发送数据:
//POST请求传递参数:需要将参数写到send方法中.
xhr.send("id=3&name=张三");
}