您的位置:首页 > 运维架构 > Tomcat

tomcat、servlet、jsp的一个典型例子

2015-06-22 09:34 627 查看
1.环境准备:

A.eclipse:luna 4.4.2版本

B.tomcat和tomcat 插件:tomcat版本为8.0.21;Plugin版本为3.3.4.1,通过eclipse的marketplace安装

2.设置eclipse中tomcat插件:通过eclipse的preferences设置

3.例子内容:

A.登录页面输入username和password

B.如果输入为weian和1234,则进入登录成功页面,否则进入登录失败页面

4.具体实现

A.编写登录、成功、失败三个页面

A.1.登录页面:

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

        <title>登录页面</title>

</head>

<body>

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

            <table>

                <tr>

                    <td>请输入用户名:</td>

                    <td><input type="text" name="username"></td>

                </tr>

                <tr>

                    <td>请输入密码:</td>

                    <td><input type="password" name="password"></td>

                </tr>

                <tr>

                    <td><input type="reset" value="重填"></td>

                    <td><input type="submit" value="登录"></td>

                </tr>

            </table>

        </form>

</body>

</html>

A.2.登录成功页面:

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>登录成功</title></head>

<body>

登录成功,欢迎进入weian的个人空间(http://blog.csdn.net/weifaqiang/article/details/46562947)!

</body>

</html>

A.3.登录失败页面:

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>Insert title here</title>

</head>

<body>

fail to login and try again.

</body>

</html>

B.编写Web.xml:

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaeehttp://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">

  <display-name>test</display-name>

<servlet>

        <servlet-name>LoginServlet</servlet-name>

        <servlet-class>

             com.edu.login.LoginServlet </servlet-class>

    </servlet>

 

    <servlet-mapping>

        <servlet-name>LoginServlet</servlet-name>

        <url-pattern>/login</url-pattern>

    </servlet-mapping>

  <welcome-file-list>

    <welcome-file>index.html</welcome-file>

    <welcome-file>index.htm</welcome-file>

    <welcome-file>index.jsp</welcome-file>

    <welcome-file>default.html</welcome-file>

    <welcome-file>default.htm</welcome-file>

    <welcome-file>default.jsp</welcome-file>

  </welcome-file-list>

</web-app>

C.编写Servlet:

package com.edu.login;

import java.io.IOException;

import java.io.PrintWriter;

import javax.servlet.ServletConfig;

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 = 1L;

 

 private ServletConfig config;

 public void init(ServletConfig config) throws ServletException

    {

        this.config = config;

    } 

 public void doGet(HttpServletRequest req, HttpServletResponse resp)

                throws ServletException,IOException

    {

      

        resp.setContentType("text/html;charset=gb2312");

      

        String name=req.getParameter("username");

        String pwd=req.getParameter("password");

        if(name!=null && pwd!=null && name.equals("weian") &&

           pwd.equals("1234"))

        {

            resp.sendRedirect("success.html");

        }

        else

        {

            resp.sendRedirect("fail.html");

        }

      

    }

  

    public void doPost(HttpServletRequest req,HttpServletResponse resp)

               throws ServletException,IOException

    {

        doGet(req,resp);

    }

}

5.总结

基于tomcat、通过其servlet container,阐述了tomcat的经典用法,为SSH架构打好基础。


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: