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

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架构打好基础。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值