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目录结构:

    运行效果:

  • 相关阅读:
    课时28:文件:因为懂你,所以永恒
    课时27:集合:在我的世界里,你就是唯一
    课时26:字典:各种内置方法
    课时25:字典:当索引不好用时
    课时24:递归:汉诺塔
    课时23:递归:这帮小兔崽子
    课时22:函数:递归是神马
    有序表查找-折半查找
    C#
    C#
  • 原文地址:https://www.cnblogs.com/caoguo/p/5264541.html
Copyright © 2011-2022 走看看