本工程的功能是实现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. linux 命令行下更换软件源

    首先备份默认源: sudo cp /etc/apt/sources.list /etc/apt/sources.list.old 清空默认源: sudo cat /dev/null > /etc ...

  2. Spring mvc-异常javax.servlet.ServletException: Could not resolve view with name 'xxx' in servlet with name 'spring'

    最近使用spring mvc开发项目,遇到一个问题: javax.servlet.ServletException: Could not resolve view with name 'ok' in ...

  3. JACASCRIPT--的奇技技巧的44招

    JavaScript是一个绝冠全球的编程语言,可用于Web开发.移动应用开发(PhoneGap.Appcelerator).服务器端开发(Node.js和Wakanda)等等.JavaScript还是 ...

  4. Linux命令:修改文件权限命令chmod、chgrp、chown详解

    Linux系统中的每个文件和目录都有访问许可权限,用它来确定谁可以通过何种方式对文件和目录进行访问和操作. 文件或目录的访问权 限分为只读,只写和可执行三种.以文件为例,只读权限表示只允许读其内容,而 ...

  5. servlet监听器Listener(理论+例子)

    Listener采用了观察者模式(24种模式之一),Listener是servlet的监听器,他可以监听客户端的请求.服务器端的操作等, 通过监听器,可以自动激发一些操作.比如:监听在线用户数量 当增 ...

  6. BZOJ 1103: [POI2007]大都市meg

    1103: [POI2007]大都市meg Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2189  Solved: 1160[Submit][Sta ...

  7. js-JavaScript高级程序设计学习笔记14

    第十六章 HTML5脚本编程 1.跨文档消息传递.简称XDM,指的是来自不同域的页面间传递消息. XDM的核心是postMessage()方法,接收两个参数,一条消息和消息接收方来自哪个域的字符串. ...

  8. CSS中的a标签几个访问状态记录

    a:link {color: #FF0000}     /* 未访问的链接 */a:visited {color: #00FF00}  /* 已访问的链接 */a:hover {color: #FF0 ...

  9. android布局学习-使用FrameLayout和LinearLayout制作QQ空间底部导航栏

    [声明:本博客通过学习“J灬叶小超 ”博客而写,链接:http://www.cnblogs.com/yc-755909659/p/4288260.html] --------------------- ...

  10. Sublime Text以及Package Control安装方法

    官方下载:Sublime Text 中国论坛:Sublime 论坛 Sublime Text 是一个代码编辑器,具有漂亮的用户界面和强大的功能,并且它还是一个跨平台的编辑器,同时支持Windows.L ...