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

Java Servlet JSP编程(一)

运行效果:

Java Servlet JSP编程(一)

Java Servlet JSP编程(一)