700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > java web 登录_javaWeb实现登录功能

java web 登录_javaWeb实现登录功能

时间:2018-09-30 22:33:01

相关推荐

java web 登录_javaWeb实现登录功能

1.三要素

(1) 入口 就是我们所在的页面

入口到处理的数据请求会出现乱码,用request.SetCharacterEncoding("UTF-8");来解决,仅仅是用用于Post请求。

(2)处理 就是在Servlet中执行的内容

处理到出口之间也会出现乱码,用response.SetCharacterEncoding("UTF-8")来解决

(3)出口 就是我们跳转的页面

web.xml配置如下

xmlns="/xml/ns/javaee"

xmlns:xsi="/2001/XMLSchema-instance"

xsi:schemaLocation="/xml/ns/javaee

/xml/ns/javaee/web-app_2_5.xsd">

LoginServlet

com.beiwo.servlet.LoginServlet

LoginServlet

/LoginServlet

index.jsp

2.首先写一个登录界面

Login.html

账号:

密码:

//当我们将鼠标放置登录按钮上面时会出现用户名跟密码,不安全

登录

3.点击登录之后会进入servlet

package com.beiwo.servlet;

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 LoginServlet extends HttpServlet {

/**

* Constructor of the object.

*/

public LoginServlet() {

super();

}

/**

* Destruction of the servlet.

*/

public void destroy() {

super.destroy(); // Just puts "destroy" string in log

// Put your code here

}

/**

* The doGet method of the servlet.

*

* This method is called when a form has its tag value method equals to get.

*

* @param request the request send by the client to the server

* @param response the response send by the server to the client

* @throws ServletException if an error occurred

* @throws IOException if an error occurred

*/

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

System.out.println("进入了doGet方法");

String name = request.getParameter("username");

String password = request.getParameter("pwd");

System.out.println("用户名:" + name + "密码:" + password);

request.setAttribute("username", name);

request.setAttribute("pwd", password);

//跳转并且传递参数

request.getRequestDispatcher("Success.jsp").forward(request, response);

//html文件不能够传递参数,只有jsp文件可以传递参数

//跳转不带参数

response.sendRedirect("Success.jsp");

}

/**

* The doPost method of the servlet.

*

* This method is called when a form has its tag value method equals to post.

*

* @param request the request send by the client to the server

* @param response the response send by the server to the client

* @throws ServletException if an error occurred

* @throws IOException if an error occurred

*/

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

System.out.println("进入了doPost方法");

//拿到用户名和密码

String name = request.getParameter("username");

String password = request.getParameter("pwd");

System.out.println("用户名:" + name + "密码:" + password);

//保存用户名和密码

request.setAttribute("username", name);

request.setAttribute("pwd", password);

//跳转并且传递参数

request.getRequestDispatcher("Success.jsp").forward(request, response);

//跳转不带参数

//response.sendRedirect("Success.jsp");

}

/**

* Initialization of the servlet.

*

* @throws ServletException if an error occurs

*/

public void init() throws ServletException {

// Put your code here

}

}

4.跳转到另外一个页面,并且拿到 用户名跟密码

//在这里首先要将编码格式改为utf-8,否则会出现乱码

Success.html

//当我们拿数据的时候要用${}

用户名:${username} 密码:${pwd}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。