1.三要素

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

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

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

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

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

web.xml配置如下

xmlns="http://java.sun.com/xml/ns/javaee"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/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}

java web 登录_javaWeb实现登录功能相关推荐

  1. Java Web 实现 QQ第三方登录

    java web 实现QQ第三方登录功能 首先点击该链接进入腾讯开放平台 ,注册成为开发者 2.注册成功后到这个页面 3.创建web应用 4.填写基本信息(以前的这里会让你填写回调地址 ,现在改了) ...

  2. java web+前端实现 人脸登录功能(专注细节)

    本次的博客是自己软件工程课设的作业,写博客是为了记录自己的学习过程,然后总结,同时可以给需要的人作参考. 1.在做这个项目的时候,也是个大三学生,所以懂得并不多,因此在网上看见了一些相关的资源,但是都 ...

  3. java web创意_javaweb有什么能做的项目,最好比较有创意一点

    有创意的JavaWeb项目 java 本人自己以想好项目,先谁有ajaxfileupload.js插件实现无刷新上传文件的例子 ?其他方法也行 邮箱 liuzhiming15955441@qq. co ...

  4. java web弹幕_JavaWeb - AcFun弹幕视频网 - 认真你就输啦 (?ω?)ノ- ( ゜- ゜)つロ

    001-Servlet-学习内容介绍 002-IDEA-IDEA的下载与安装 003-IDEA-IDEA创建Java项目 004-IDEA-IDEA的基本设置 005-HTTP-浏览器和服务器的交互流 ...

  5. java web日期_java-web——第十一课 时间类

    前言: 在我们学习java web之中,实体类的属性经常会遇到时间类 时间类也是一个很重要的类, 首先我们先了解一下经常使用的时间类 经常使用的时间类也就是这三个类 具体的可以查阅api 我们这章主要 ...

  6. java web实验_javaweb实验报告

    javaweb实验报告 甘肃政法学院本科生实验报告(一)姓名:学院:计算机科学学院专业: 计算机科学与技术班级实验课程名称:实验日期:2012 年 04 月 9 日指导教师及职称实验成绩:开课时间:2 ...

  7. java web网站集成新浪微博登录

    今天要给网站添加第三方登录的功能,研究了下新浪微博登录的接口,本来想使用它提供的那种js代码库的方式,结果始终搞不定,由于不精通js只能够放弃.下载了个java的SDK包:weibo4j-oauth2 ...

  8. java web 微博系统,java web网站集成新浪微博登录

    给网站添加第三方登录的功能,研究了下新浪微博登录的接口,本来想使用它提供的那种js代码库的方式,结果始终搞不定,由于不精通js只能够放弃.下载了个java的SDK包:weibo4j-oauth2-be ...

  9. java web 邮件_JavaWeb实现邮件发送接收功能

    一.邮件开发涉及到的一些基本概念 1.1.邮件服务器和电子邮箱 要在Internet上提供电子邮件功能,必须有专门的电子邮件服务器.例如现在Internet很多提供邮件服务的厂商:sina.sohu. ...

最新文章

  1. 问题集锦(36-40)
  2. python运行程序-一文看懂python程序的执行过程
  3. Android之内核学习笔记
  4. 比较完整的URL验证
  5. HDU2036 改革春风吹满地【多边形面积】
  6. python怎么读取excel-python 读取 Excel
  7. ansible+packer+terraform在aws上布署web服务器
  8. 华为IS-IS基础配置
  9. 为什么训练时测试准确率大幅度波动_Nature Mach Intell|类药性预测准确率有极限...
  10. 用python语言提取千位数_C语言怎样提取一个数的十位个位百位千位?
  11. 界面开发控件DotNetBar for WPF教程:MobileRibbon快速入门指南
  12. java bitmap位图_BitMap位图与海量数据的理解
  13. SHN-PEG2000-Pyrene,Pyrene-PEG2000-NHS
  14. Python实现伽马矫正
  15. 摄氏温度转化为华氏温度代码
  16. Win7网络和共享中心显示“依赖服务或组无法启动”,无法连接网络
  17. c语言 虚拟示波器软件下载,虚拟示波器软件(示波器工具)V3.1 官方版
  18. UITextView - 2
  19. 计算机二级考试内容是什么
  20. Ant入门教程之常用命令

热门文章

  1. 什么是CSS特异性及其工作方式?
  2. 计算机术语hpa,hpa(计算机术语)_百度百科
  3. 四川岳池2021年高考成绩查询,2021年岳池中学升学率高不高?
  4. 大学毕业必须考的计算机证书,大学4年,一定要考的4大证书,不考毕业后悔死...
  5. 一文讲透支付宝沙箱的基本应用
  6. 彻底解决Eclipse自动补全变量名问题的方法步骤
  7. arttemplate+ajax 转 vue
  8. 七彩cms云转码_七彩CMS 2019云转码完全开源版本 程序源码带安装教程
  9. JAVA三角形边长定义_Java编译:定义三角形的三条边长a=4,b=8.54,c=4.44;求三角形的周长d...
  10. 神经网络论文研究-图像处理方向4-geoglenet