这节课实现了什么?
- User.java
- 用户对象
- UserDao.java
- 数据流
- UserServlet.java
- 调用servlet
- web.xml
- 配置文件
- index.jsp
- 网页编辑
项目文件
1.User.java—>建立User对象 并配上set get hashcode
在User内编写private数据 username、password
并且设置set get hashcode equals 方法
代码如下:
package com.zking.bean;
public class User {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((password == null) ? 0 : password.hashCode());
result = prime * result
+ ((username == null) ? 0 : username.hashCode());
return result;
}
@Override
//equals判断username and password 是否相同 相同的话就不能登录
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
User other = (User) obj;
if (password == null) {
if (other.password != null)
return false;
} else if (!password.equals(other.password))
return false;
if (username == null) {
if (other.username != null)
return false;
} else if (!username.equals(other.username))
return false;
return true;
}
}
2.UserDao.java
package com.zking.dao;
import com.zking.bean.User;
public class UserDao {
public boolean login(User user){
User u = new User();
u.setUsername("123");
u.setPassword("xiekun");
//只有正确的输入用户名:123,密码:xiekun 才能登陆成功
return u.equals(user);
}
}
3.UserServlet.java–>调用servlet
package com.zking.action;
import com.zking.bean.User;
import com.zking.dao.UserDao;
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;
@SuppressWarnings("serial")
public class UserServlet extends HttpServlet{
public void service (HttpServletRequest request, HttpServletResponse response) throws IOException,ServletException{
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
//获得URL地址
StringBuffer str = request.getRequestURL();
//out.println(str+" "); //out print ----> http://localhost:8899/testweb/login.action
//截取路径 path = login
String path = str.substring(str.lastIndexOf("/")+1, str.lastIndexOf("."));
//点击登录 即可输出path
System.out.println(path);
if("login".equals(path)){
//username and password was writtern in username and pwd
String username = request.getParameter("username");
String pwd = request.getParameter("password");
User u = new User();
u.setUsername(username);
u.setPassword(pwd);
UserDao ud = new UserDao();
boolean flag = ud.login(u);
if(flag){
out.println("登陆成功!");
}else{
out.println("用户名或者密码错误!");
}
}
else if("register".equals(path)){
}else{
out.println("bilibili");
}
}
}
4.web.xml—>配置servlet部分
<servlet>
<servlet-name>login</servlet-name>
<servlet-class>com.zking.action.UserServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/login.action</url-pattern>
</servlet-mapping>
5. index.jsp–>网页显示部分
<html>
<head>
<title>login in</title>
</head>
<body>
<form action="/testweb/login.action">
<table>
<tr align="center">
<td colspan ="4" style = "color:red;"><h1>login in</h1></td>
</tr>
<tr>
<td ><label for="uname">user:</label></td>
<td colspan ="3"><input typle ="text" id="uname" name="username" value="QQ/email/phone"></td>
</tr>
<tr>
<td ><label for="upassword">password:</label></td>
<td colspan ="3"><input typle ="text" id="upassword" name="password" ></td>
<!-- td.name是框框的名字 td.id是与label的upassword匹配-->
</tr>
<tr align="center">
<td colspan ="4"><input type="submit" value="login o"></td>
</tr>
</table>
</form>
</body>
</html>
新手学习中,如有错误,烦请斧正!