码迷,mamicode.com
首页 > Web开发 > 详细

jsp的示例

时间:2017-03-17 15:08:07      阅读:260      评论:0      收藏:0      [点我收藏+]

标签:serial   dtd   val   url   结构   sub   doc   页面   oid   

目录结构

技术分享

login.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>jsp测试页面</title>
</head>
<body>
    <form method="post" action="login">
        username:
        <input type="text" name="username"><br/>
        password:
        <input type="text" name="pwd"><br/>
        <input type="submit" value="提交">
    </form>
</body>
</html>

 

hello.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>欢迎来到菜鸟营地</title>
</head>
<body>
    hello servlet
</body>
</html>

 

 

FirstJsp.java

public class FirstJsp extends HttpServlet {
    private static final long serialVersionUID = 1L;
    
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        String name = req.getParameter("username");
        String pwd = req.getParameter("pwd");
        if((name != null)&&(name.trim().equals("jsp"))){
            if((pwd != null)&&(pwd.trim().equals("1"))){
                
                String login = "hello.html";
                resp.sendRedirect(login);
                return;
            }
        }
    }
    
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        this.doGet(req, resp);
    }
  }

web.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <servlet>
      <servlet-name>log</servlet-name>
      <servlet-class>FirstJsp</servlet-class>
  </servlet>
  <servlet-mapping>
      <servlet-name>log</servlet-name>
      <url-pattern>/login</url-pattern>
  </servlet-mapping>
</web-app>

 

输入访问地址

http://localhost:8080/jsptest/login.jsp

技术分享

点击提交

技术分享

注意: 访问地址是直接访问的jsp 点击提交之后 servlet容器 调用servlet 根据from表单中action 去找web.xml的url地址

然后去Java文件中运行处理方法  将处理结果返回给servlet 容器

 

jsp的示例

标签:serial   dtd   val   url   结构   sub   doc   页面   oid   

原文地址:http://www.cnblogs.com/zjf6666/p/6565774.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!