zoukankan      html  css  js  c++  java
  • Java Servlet JSP编程(一)

    最近想学学java编程,java现在的应用还是挺广泛的,有必要学习一下。

    # index.jsp
    
    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
    <!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=ISO-8859-1">
    <title>ServletValue</title>
    </head>
    <body>
    <form method="get" action="/JavaWeb/HelloWorld" >
    Name:<input type="text" name="name" /><br />
    Age:<input type="text" name="age" /><br />
    <input type="submit"  value="submit" />
    
    </form>
    </body>
    </html>
    # com.seller.servlets   HelloWorld.java
    package com.seller.servlets;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    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("/HelloWorld")
    public class HelloWorld extends HttpServlet {
        private static final long serialVersionUID = 1L;
           
    
        public HelloWorld() {
            super();
        }
    
    
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
            String name = request.getParameter("name");        
            String age = request.getParameter("age");
            request.setAttribute("name", name);
            request.setAttribute("age", age);
            request.getRequestDispatcher("HelloWorld.jsp").forward(request, response);
        }
    }
    # HelloWorld.jsp
    <%@ page language="java" contentType="text/html; charset=utf-8"
        pageEncoding="ISO-8859-1"%>
    <!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>ServletValue</title>
    </head>
    <body>
    
    <%
        String name = (String)request.getAttribute("name");
        String age = (String)request.getAttribute("age");
    %>
    
    
    Name: <%=name %><br />
    Age: <%=age %>
    </body>
    </html>

    eclipse目录结构:

    运行效果:

  • 相关阅读:
    Webstorm(OnlineSearch2)自定义快捷搜索API文档手册
    cargo设置国内源
    win10安装rust和编译失败的解决办法
    pycharm打开项目找不到根目录的解决办法
    VM虚拟机/Linux上网
    idea启动springboot项目突然特别慢
    (亲测有效)MacPycharm打不开的解决方法
    vue使用webpack打包失败
    使用七牛云上传文件报错incorrect region, please use up-z1.qiniup.com
    Zookeeper3.5及以上启动时8080端口被占用
  • 原文地址:https://www.cnblogs.com/caoguo/p/5264541.html
Copyright © 2011-2022 走看看