javaWeb03

摘要:java学习
你想输入的替代文字

在java中如何访问jsp的内置对象
public class MyFunction {
public void addEmp(HttpServletRequest request) {
String empno =request.getParameter(“empno”);
System.out.println(“参数~~~~”+empno);
}
}
在jsp中
<%
MyFunction mf = new MyFunction();
mf.addEmp(request);
%>
当我们进行访问
http://localhost:8080/WebDemo03/JspTojava.jsp?empno=31
我们MyFunction就能进行响应获得内容
登陆注册包含了有什么功能
注册
1、验证码
2、发送手机校验
3、绑定邮箱 点击邮箱进行激活
登陆
第三方登陆 QQ微信
找回密码

单点登陆

什么是单点登陆,就是当前有一个点进行登陆,忽略其他异常情况,只考虑校验用户名和密码

public class LoginUtil {
 public static void main(String[] args) throws Exception {
 boolean is = LoginUtil.verify(“zhangcheng”, “qaz1”);
 if (is) {
  System.out.println(“登陆成功”);
 }else {
  System.out.println(“登陆失败”);
 }
}
 public static boolean verify(String username,String password) throws Exception {
 Connection con = null;
 ResultSet rs =null;
 PreparedStatement stm = null;
 String sql = “select * from users where username=? and password=? and status=1”;
 Class.forName(“com.mysql.jdbc.Driver”);
   String url = “jdbc:mysql://localhost:3306/javaweb?useUnicode=true&characterEncoding=utf-8”;
 String sqlusername = “root”;
 String sqlpassword = “pwd”;
 con = DriverManager.getConnection(url,sqlusername,sqlpassword);
 stm = con.prepareStatement(sql);
 stm.setString(1, username);
 stm.setString(2, password);
 rs=stm.executeQuery();
 boolean isSuccess=true;
 if (rs.next()) {
  isSuccess = true;
 }else {
  isSuccess = false;
 }
  //释放资源
  rs.close();
  stm.close();
  con.close();
  return isSuccess;
 }
}

<%
//用于显示错误信息
String error = (String)request.getAttribute(“error”);
if(error!=null){
 out.println(error);
}
%>



用户名:

密码:



Cookie
刚才我们说到了登陆,登陆以后我们如何保存登陆的数据,session可以进行保存,但是session保存时候,是保存当次的会话,我们关闭了浏览器,下次是否还需要再次进行登陆?cookie解决这个问题

Cookie是什么
Cookie是一小段的文本信息,Web服务器将它发送到浏览器客户端,以文本形式保存起来,随后当浏览器下次访问该站点的时候,Web服务器可以通过Cookie来获得到上次访问的记录
Cookie存储的方式一般是一个属性和一个值的形式
Cookie数据存在磁盘上,电脑关闭后仍然存在
当你下次在访问该站点时候,我读取cookie就可以进行免登陆

Cookie的用法
回到我们的loginWebDemo项目,我们通过Cookie来保存用户名和密码

添加cookie
<%
//创建一个cookie
Cookie c1 = new Cookie(“username”,”zhangcheng”);
//设置cookie的有效期
c1.setMaxAge(6060247);
Cookie c2 = new Cookie(“password”,”qaz”);
c2.setMaxAge(60
60247);

response.addCookie(c1);
response.addCookie(c2);
//setMaxAge 方法参数为0的时候,表示命令浏览器立即删除cookie 参数为-1时候,永久保存
%>

读取cookie
{

<%
out.println(“所有cookie:
“);
Cookie[] c= request.getCookies();

if(c!=null&&c.length>0){
 for(int i=0;i<c.length;i++){
  out.println(c[i].getName()+”=”+c[i].getValue());
  out.println(“
“);
 }
}

完善login内容
登陆成功后写入cookie,下次登陆自动填充
<%
Cookie[] c= request.getCookies();

String username=null;
String password=null;

if(c!=null&&c.length>0){

    for(int i=0;i<c.length;i++){
        if(c[i].getName()!=null&&c[i].getName().equals("username")){
            username = c[i].getValue();
        }
    if(c[i].getName()!=null&&c[i].getName().equals("password")){
            password = c[i].getValue();
        }
    }
    if(username==null){
        username ="";
    }
    if(password==null){
        password ="";
    }
}



用户名:

密码:




<%
request.setCharacterEncoding(“UTF-8”);
String username = request.getParameter(“username”);
String password = request.getParameter(“password”);
if(LoginUtil.verify(username, password)){
 out.println(“即将进入主页”);
 Cookie c1 = new Cookie(“username”,username);
 Cookie c2 = new Cookie(“password”,password);
 response.addCookie(c1);
 response.addCookie(c2);
}else{
 request.setAttribute(“error”, “登陆失败!用户无效“);
 request.getRequestDispatcher(“Login.jsp”).forward(request, response);
}
%>