现在的位置: 首页 > 综合 > 正文

java web从零单排第五期《servlet》(3)

2013年07月14日 ⁄ 综合 ⁄ 共 3704字 ⁄ 字号 评论关闭

这一期主要介绍servlet与jsp的结合使用,之后进行简单的总结。

首先阐明我们想要做点什么,最简单的当然是登陆功能,输入用户名,会跳转到成功的界面,失败则跳转到失败界面。

1.编写jsp代码如下:

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'test_1.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head> 
  <body>
    <form action="/servlet/login">
     <table align="center">
      <tr>
        <td>username:</td>
        <td><input type="text" name="username" id="username"></td>
      </tr>
      
      <tr>
       <td>password:</td>
       <td><input type="text" name="password"  id="password"></td>
      </tr>
      
      <tr>
        <td><input type="submit" value="submit"></td>
        <td><input type="reset" value="reset"></td>
      </tr> 
     </table>
    </form>
  </body>
</html>

在com.test.servlet包下新建login.java,代码如下:

package com.test.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 login extends HttpServlet {
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		process(req,resp);
	}	
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		process(req,resp);
	}
	protected void process(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		PrintWriter out = resp.getWriter();
		
		String username = req.getParameter("username");//获取table中name为username
		String password = req.getParameter("password");
		
		if(username.equals("hello")&&password.equals("servlet"))
		{
			out.println("<html>");
			out.println("<body>");
			out.println("login success:"+"username: "+username+"<br>");
			out.println("password: "+password);
			out.println("<html>");
			out.println("</body>");
			out.println("</html>");
		}
		else
		{
			out.print("login false");
		}		
	}
}

web.xml文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	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">
  <display-name></display-name>	
  
  <welcome-file-list>
    <welcome-file>test_1.jsp</welcome-file>
  </welcome-file-list>
  
  <servlet>
    <servlet-name>login</servlet-name>
    <servlet-class>com.test.servlet.login</servlet-class>
  </servlet>
  
  <servlet-mapping>
    <servlet-name>login</servlet-name>
    <url-pattern>/login</url-pattern>
  </servlet-mapping>
</web-app>

运行结果:

运行成功界面:

这个例子并不难,首先输入,案提交按钮后,通过action找到com.test.servlet.login去执行里面的代码,获取username和password,再用out.println()方法输出内容即可。

再来看一个有关提交方式的问题:

<form action="login" method="get">

method=“get”是form默认的,我们称之为get提交方式,还有一种是post提交:

<form action="login" method="post">
 
这两种提交方式有什么区别呢?
1. get是从服务器上获取数据,post是向服务器传送数据。
2. get是把参数数据队列加到提交表单的ACTION属性所指的URL中,值和表单内各个字段一一对应,在URL中可以看到。post是通过HTTP post机制,将表单内各个字段与其内容放置在HTML HEADER内一起传送到ACTION属性所指的URL地址。用户看不到这个过程。
3. 对于get方式,服务器端用Request.QueryString获取变量的值,对于post方式,服务器端用Request.Form获取提交的数据。
4. get传送的数据量较小,不能大于2KB。post传送的数据量较大,一般被默认为不受限制。但理论上,IIS4中最大量为80KB,IIS5中为100KB。
5. get安全性非常低,post安全性较高。但是执行效率却比Post方法好。 

建议:
1、get方式的安全性较Post方式要差些,包含机密信息的话,建议用Post数据提交方式;
2、在做数据查询时,建议用Get方式;而在做数据添加、修改或删除时,建议用Post方式;
 
我们对比一下两种方法的地址栏:
1.采用get:
2.采用post:
所以post的安全性更好一些
 
这一期就写到这里,我们知道了jsp->servlet 而servlet->jsp会在下一期介绍,之后servlet篇就先告一段落。

 

 

抱歉!评论已关闭.