首页 > 代码库 > jsp的示例

jsp的示例

目录结构

技术分享

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="http://www.mamicode.com/提交">
    </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的示例