學學Java Servlet:建立一個簡單的Servlet

學學Java Servlet:建立一個簡單的Servletcss

項目介紹:html

建立一個簡單的WebProject項目,經過項目配置,用兩種配置方法實現servlet一個簡單的登陸登出小項目。java

登陸成功顯示成功,登陸失敗顯示失敗。so easy...web

配置簡介:myeclipse,tomcat7.0tomcat

詳細步驟:app

一、建立一個web project項目,建立的時候選擇J2EE6.0的版本eclipse

項目結構圖:jsp

而後下一步、結束就好。接着寫三個頁面,登陸、成功、失敗頁面。(注意編碼)ide

login.jsp代碼:post

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
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 'login.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="<%=basePath%>login" method="post">
    用戶:<input type="text" name="name"/><br>
    密碼:<input type="password" name="passwd"/><br>
    <input type="submit" value="提交">
    </form>
  </body>
</html>

success.jsp代碼:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
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 'success.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>
    登陸成功!
  </body>
</html>

fail.jsp代碼:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
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 'fail.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>
    賬號或密碼錯誤,登陸失敗!<br>
<a href="<%=basePath%>">返回登陸</a>    
  </body>
</html>

注:根據web的版本能夠不一樣的實現servlet的訪問,如今3.0版本的居多,也少了更多的配置信息,用註解顯得更加的方便靈活。web.xml版本在Myeclipse裏面能夠很直接的看到:

還有就是經過具體的配置文件信息,也明確的寫明瞭web2.5和web3.0的版本信息:

web.xml2.5配置信息:

<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">

</web-app>

web.xml3.0配置信息

<web-app version="3.0" 
	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_3_0.xsd">

</web-app>

版本的選擇也能讓咱們用不一樣的方式來開發Servlet,下面咱們就用兩種方法來寫這個Demo。

方法一:web3.0如下版本的傳統web.xml配置

一、web.xml的配置:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
	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_3_0.xsd">
  <display-name></display-name>	

  <!-- 加載的默認首頁 -->
  <welcome-file-list>
    <welcome-file>/WEB-INF/page/login.jsp</welcome-file>
  </welcome-file-list>
  
  <!-- 配置Servlet -->
  <servlet>
  	<!-- Servlet名:在配置文件中惟一 -->
  	<servlet-name>LoginServlet</servlet-name>
  	<!-- Servlet對應的類 -->
	<servlet-class>com.frend.LoginServlet</servlet-class>
  </servlet>
  <servlet-mapping>
  	<!-- Servlet名對應前面 -->
  	<servlet-name>LoginServlet</servlet-name>
  	<!-- /login:請求的url -->
  	<url-pattern>/login</url-pattern>
  </servlet-mapping>
</web-app>

二、Servlet類:

package com.frend;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginServlet extends HttpServlet {

	private static final long serialVersionUID = 5798229038714266939L;

	@Override
	public void init() throws ServletException {
	}

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("utf-8");//設置編碼格式以支持中文
		
		String name = request.getParameter("name");//獲取帳戶名
		String passwd = request.getParameter("passwd");//獲取密碼
		
		System.out.println("*****帳戶:"+name+",密碼:"+passwd);//打印帳戶密碼
		
		RequestDispatcher dispatcher = null;
		//驗證帳戶名和密碼
		if(!"frend".equals(name) || !"abc123".equals(passwd)){
			dispatcher = request.getRequestDispatcher("/WEB-INF/page/fail.jsp");//獲取RequestDispatcher對象
		}else{
			dispatcher = request.getRequestDispatcher("/WEB-INF/page/success.jsp");//獲取RequestDispatcher對象
		}
		dispatcher.forward(request, response);//向目的路徑跳轉
	}

	@Override
	public void destroy() {
	}
	
}

 

方法二:web3.0以上版本的註解配置

一、web.xml的配置:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
	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_3_0.xsd">
  <display-name></display-name>	
  
  <!-- 加載的默認首頁 -->
  <welcome-file-list>
    <welcome-file>/WEB-INF/page/login.jsp</welcome-file>
  </welcome-file-list>
</web-app>

二、Servlet類:

/**
 * @(#) LoginServlet.java	2016-7-20 上午10:29:48
 * 
 * Copyright (c) 2014-2015 Diyvc, Inc.
 * 103 Sports Road, Victoria Plaza tower 1104, Tianhe District Guangzhou City, China.
 * All rights reserved.
 *
 */
package com.frend;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(name="LoginServlet",urlPatterns="/login")
public class LoginServlet extends HttpServlet {

	private static final long serialVersionUID = 5798229038714266939L;

	@Override
	public void init() throws ServletException {
	}

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("utf-8");//設置編碼格式以支持中文
		
		String name = request.getParameter("name");//獲取帳戶名
		String passwd = request.getParameter("passwd");//獲取密碼
		
		System.out.println("*****帳戶:"+name+",密碼:"+passwd);//打印帳戶密碼
		
		RequestDispatcher dispatcher = null;
		//驗證帳戶名和密碼
		if(!"frend".equals(name) || !"abc123".equals(passwd)){
			dispatcher = request.getRequestDispatcher("/WEB-INF/page/fail.jsp");//獲取RequestDispatcher對象
		}else{
			dispatcher = request.getRequestDispatcher("/WEB-INF/page/success.jsp");//獲取RequestDispatcher對象
		}
		dispatcher.forward(request, response);//向目的路徑跳轉
	}

	@Override
	public void destroy() {
	}
	
}

頁面截圖:

一、失敗情景

二、成功情景

 

小結:

傳統的web.xml適合配置文件控制,可是servlet多的狀況下徹底不方便管理;

註解方式簡單方便,很適用開發。

相關文章
相關標籤/搜索