本工程的功能是实现Javaweb的servlet身份验证

一下是login.html文件中的代码

<!DOCTYPE html>
<html>
<head>
<title>login.html</title> <meta name="keywords" content="keyword1,keyword2,keyword3">
<meta name="description" content="this is my page">
<meta name="content-type" content="text/html; charset=GBK"> <!--<link rel="stylesheet" type="text/css" href="./styles.css">--> <script type="text/javascript">
function check(){
//获取控件内容
var loginname = document.getElementById("loginname").value;
if(loginname == ""){
alert("用户名不能为空");
document.getElementById("loginname").focus();//获取焦点
return false;
} var password = document.getElementById("password").value;
if(password == ""){
alert("密码不能为空");
document.getElementById("password").focus();
return false;
} //验证成功
document.loginform.submit();
}
</script> </head> <body>
<center>
<h2>登陆页面</h2>
<br>
<!-- html数据由两种传输方式 1.get 从地址栏传递 2.form表单传输
form代表表单
--action属性代表提交的url
action="login.do",那么在web.xml里面定义<servlet-mapping>的<url-pattern>
的时候也是login.do
--method属性代表提交表单的方式,http里面重点是get和post,默认get方式提交
--name属性给表单其名字
--id属性代表唯一标示该表单的名字,主要是javascript脚本使用
-->
<form action="login.do" method="get" name="loginform" id="loginform">
<table>
<tr>
<td>登录名:</td>
<td><input type="text" name="loginname" id="loginname"/></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="password" id="password"/></td>
</tr>
</table>
<table>
<tr>
<td><input type="button" value="提交" onclick="check();"></td>
&nbsp;&nbsp;
<td><input type="reset" value="重置"></td>
</tr>
</table>
</form> </center>
</body>
</html>

以下代码是LoginServlet.java中的代码

package org.common.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.Properties; 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. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
} /**
* The doGet method of the servlet. <br>
*
* 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 方法...");
// //1.接收前台传递过来的参数
// Enumeration enums = request.getParameterNames();
// while(enums.hasMoreElements()){
// System.out.println(enums.nextElement());
//
// } //转换编码的第2种方式,配合doPost()方法使用
request.setCharacterEncoding("GBK"); //提交的name可以在后台使用request.getParameter("loginname")获取值
String loginname = request.getParameter("loginname");
System.out.println("转换前loginname:" + loginname);
//String password = request.getParameter("password"); //把loginname这个字符串转成GBK,前提你要确定编码
loginname = new String(loginname.getBytes("iso-8859-1"),"GBK");
System.out.println("转换后loginname:" + loginname);
String password = request.getParameter("password"); //properties文件是java的默认配置文件,以key-value的形式存储数据
//增加了一个user.properties文件存储用户名密码
Properties pro = new Properties();
//load方法从输入流中读取属性列表(键和元素对)
pro.load(this.getClass().getResourceAsStream("/user.properties"));
//System.out.print(pro); response.setContentType("text/html;charset=GBK");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>"); //out.print(" loginname: " + loginname);
//out.print(" password: " + password);
if(loginname.equals(pro.getProperty("loginname"))
&& password.equals(pro.getProperty("password"))){
out.println(" 欢迎["+pro.getProperty("username")+"]登陆");
}else{
out.println("用户名密码错误");
} out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
} /**
* The doPost method of the servlet. <br>
*
* 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 { this.doGet(request, response);
} /**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
} }

doGet()方法不安全,所以尽量使用doPost()方法

JavaWeb学习笔记——开发动态WEB资源(五)servlet身份验证的更多相关文章

  1. JavaWeb学习笔记——开发动态WEB资源(一)Java程序向浏览器输出数据

    开发一个动态web资源,即开发一个Java程序向浏览器输出数据,需要完成以下2个步骤: 1.编写一个Java类,实现Servlet接口 开发一个动态web资源必须实现javax.servlet.Ser ...

  2. JavaWeb学习笔记——开发动态WEB资源(六)ServletConfig和ServletContext

    1.只有在第一次请求服务器产生实例的时候才会调用init()方法,有一种办法能在服务器一启动的时候就加载init()方法. 即服务器启动即加载Servlet,且按数字大小顺序实例化Servlet. 方 ...

  3. JavaWeb学习笔记——开发动态WEB资源(二)HelloWord

    该工程的功能是在页面上输出一段话 首先在src里面新建一个class,在interface里面添加javax.servlet.Servlet 以下是HelloServlet.java中的代码: pac ...

  4. JavaWeb学习笔记——开发动态WEB资源(八)cookies和httpsession

    会话: cookies: (1)cookies是WEB服务器发送到浏览器的简短文本信息 (2)cookies可以禁用 httpsession: 一次会话是从你打开浏览器开始到你关闭浏览器结束 提供一种 ...

  5. JavaWeb学习笔记——开发动态WEB资源(七)bookapp

    该工程的功能是实现一个bookapp 1.开发注册页面,注册使用properties文件,存储在classpath跟路径 2.注册成功跳转到登录页面 3.输入用户名密码登录,登录成功跳转到book显示 ...

  6. JavaWeb学习笔记——开发动态WEB资源(四)打印当前使用的是get方法

    该工程的名称是testhttp,功能是在页面中表格打印浏览过程中的相关头信息. 新建一个工程,然后在这个工程里面新建一个servlet,这样便可以省去编写web.xml的过程 以下是TestHttpS ...

  7. JavaWeb学习笔记——开发动态WEB资源(三)显示当前时间

    该工程的功能是实现在页面中显示当前的时间 以下的代码是HelloServlet.java中的代码 package helloapp2; import java.io.IOException; impo ...

  8. Ruby学习笔记4: 动态web app的建立

    Ruby学习笔记4: 动态web app的建立 We will first build the Categories page. This page contains topics like Art, ...

  9. Ruby学习笔记6: 动态web app的建立(3)--多Model之间的交互

    We first built a static site which displayed a static image using only a Controller and a View. This ...

随机推荐

  1. HIbernate的脏数据检测和延缓加载

    脏数据监测: 在一个事务中,加载的数据,除了返回给用户之外,会复制一份在session中,在事务提交时,会用session中的备份和用户的数据进行比对,如果用户的数据状态改变, 则用户的数据即为:脏数 ...

  2. ESPCMS基本导航操作

    Espcms和dedecms一样,是用来建企业站的cms程序,功能强大,稳定,可以帮助您快速.便捷地新建一个企业网站.无忧主机向您推荐无忧主机php虚拟主机. 我们可以通过espcms设置来去掉比如购 ...

  3. asp.net mvc 多级目录结构

    ikmb@163.com ASP.NET MVC默认的文件组织和URL访问都是一级,我们通常要将一个功能模块组织到一个目录下.方法是:1.文件组织 分别在Controllers和Views文件夹下建议 ...

  4. 6.7-3将数组arr中索引值为2的元素替换为“bb”

    package shuzu; import java.util.Arrays; public class TH { public static void main(String[] args) { / ...

  5. Web的结构组件

    HTTP代理服务器的作用 Web安全,应用集成,性能优化 1. 代理 In computer networks, a proxy server is a server (a computer syst ...

  6. 关于volatile的可见性问题

    volatile的定义是:volatile是轻量级的synchronized,它在多处理器开发中保证了共享变量的‘可见性’,可见性的意思是当一个线程修改一个共享变量时,另外一个线程能够读到这个修改的值 ...

  7. 控件 UI: VisualState, VisualStateManager, 控件的默认 UI

    VisualState 和 VisualStateManager 控件的默认 Style, ControlTemplate, VisualState 示例1.演示“VisualState 和 Visu ...

  8. bzoj4418&&bzoj4419&&bzoj4420:SHOI2013Day2题解

    这三题截止现在(2016.3.11)窝居然都是跑的最快的……可啪…… T1 bzoj4418 这题叫做扇形面积并,看到这个名字我就方了,因为我不会计算几何啊QAQ 一看题目,发现是傻逼题……(雾) 又 ...

  9. c# 监听文件夹动作

    static FileSystemWatcher watcher = new FileSystemWatcher(); /// <summary>        /// 初始化监听     ...

  10. Android热身:通过网络获取资源并更新UI组件

    Android热身:通过网络获取资源并更新UI组件 目标 点击"发送请求"按钮,下载某网页的html源码,并显示在TextView控件上:点击"清空",清除Te ...