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

    运行效果:

  • 相关阅读:
    C语言面试题——寻找错误
    C语言的声明解释的在线工具——cdecl
    C语言面试题——指针运算
    const 指针与指向const的指针
    C语言复杂声明解释
    poj1248
    poj1750
    poj1484
    poj1853
    poj1575
  • 原文地址:https://www.cnblogs.com/caoguo/p/5264541.html
Copyright © 2011-2022 走看看