1: 我们可以将Servlet看做嵌套了HTML代码的java类。可以将jsp看作嵌套了java代码的HTML页面。
Code:
package com.hzy.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginServlet extends HttpServlet
{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
resp.setContentType("text/html");
String username = req.getParameter("username");
String password = req.getParameter("password");
PrintWriter out = resp.getWriter();
out.println("<html><head><title>login</title></head><body>");
out.println("<h2>username:" + username + "<br>password:" + password+"</h2>");
out.println("</body></html>");
out.flush();
}
}
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath =
request.getScheme() + "://" + request.getServerName() + ":"
+ request.getServerPort() + path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'login.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="Login">
username:
<input type="text" name="username">
<br>
password:
<input type="password" name="password">
<br>
<input type="submit" value="submit">
<input type="reset" value="reset">
</form>
</body>
</html>