学学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多的状况下彻底不方便管理;

注解方式简单方便,很适用开发。

相关文章
相关标签/搜索