文章目录
前言
例如:学习javaweb过程笔记,遇到了较多错误,整理一下思路
一、登录注册思路
1、前端页面(登录+注册)
2、根据前端元素创造实体类
3、连接后台代码(jsp发送post请求到Servlet,添加注解,完成功能开发)
4、设计工具类,使用JDBC连接数据库,使用properties文件存储私有数据
二、前端页面
1.Login
代码如下(示例):
其中action使用了EL表达式,${}内的内容表示绝对路径,(/Login)是一级目录,对应了Servlet中@webServlet中URLPatterns中的路径。此时,form表单发送的是post请求,则在Servlet内应重写doPost方法
<%--
Created by IntelliJ IDEA.
User: limerence
Date: 2022/11/14
Time: 15:21
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登录</title>
<link href="../css/login.css" rel="stylesheet">
</head>
<body>
<div id="loginDiv">
<form action="${pageContext.request.contextPath}/Login" id="form" method="post">
<h1 id="loginMsg">LOGIN IN</h1>
<p>Username:<input id="username" name="username" type="text"></p>
<p>Password:<input id="password" name="password" type="password"></p>
<div id="subDiv">
<input type="submit" class="button" value="login up">
<input type="reset" class="button" value="reset">
<a href="register.jsp">没有账号?点击注册</a>
</div>
</form>
</div>
</body>
</html>
2.Register
代码如下(示例):
需要注意的是,action与login一样,使用了EL表达式,同为post请求。
<%--
Created by IntelliJ IDEA.
User: limerence
Date: 2022/11/16
Time: 10:06
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java"
pageEncoding="UTF-8" %>
<html>
<head>
<title>注册界面</title>
<style>
body {
background-size: cover;
background-image: url("../imgs/1.jpg");
}
</style>
</head>
<body>
<div class="form-div">
<div class="reg-content">
<h1>欢迎注册</h1>
</div>
<form id="reg-form" action="${pageContext.request.contextPath}/Register" method="post">
<table>
<tr>
<td>用户名:</td>
<td class="inputs">
<input name="username" type="text" id="username">
</td>
</tr>
<tr>
<td>密码:</td>
<td class="inputs">
<input name="password" type="password" id="password">
</td>
</tr>
<tr>
<td>确认密码:</td>
<td class="inputs">
<input name="password1" type="password" id="password1">
</td>
</tr>
<tr>
<td>性别:</td>
<td class="inputs">
<input type="radio" id="male" name="gender" value="男">男
<input type="radio" id="female" name="gender" value="女">女
</td>
</tr>
<tr>
<td>职业:</td>
<td class="inputs">
<select id="group" name="profession">
<option value="测试">测试</option>
<option value="前端">前端</option>
<option value="后端">后端</option>
<option value="经理">经理</option>
</select>
</td>
</tr>
<tr>
<td>兴趣爱好:</td>
<td class="inputs">
<select name="favor" id="favor">
<option value="音乐"