【JavaWeb第1次上机练习】安装Tomcat并在本地浏览器成功运行第一个Hello world网站_科大云炬的博客-CSDN博客
【第2次JavaWeb上机练习】_科大云炬的博客-CSDN博客
第3次 JavaWeb上机练习:Servlet基础作业_科大云炬的博客-CSDN博客
第4次JavaWeb上机练习——ServletConfig接口的常用方法_科大云炬的博客-CSDN博客
Javaweb第5次上机练习(获取HTTP的请求参数)_科大云炬的博客-CSDN博客
功能:从前端form表单接收post请求中的参数,并传递给后端的jsp脚本提示欢迎信息
- package com.zr;
-
- 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 ServletCkde extends HttpServlet {
-
-
- public ServletCkde() {
- super();
- }
-
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
-
- //指定浏览器汉字的编码格式
- response.setContentType("text/html;charset=utf-8");
- //指定服务器的汉字编码格式
- request.setCharacterEncoding("utf-8");
- //1.获取表单中提交的名字
- String usernameString =request.getParameter("username");
- String passwordString =request.getParameter("password");
- //2.跳转到注册成功页面,并且把请求响应参数传递过去
- //3.通过request域把获取到的表单数据写入request域中,并通过getRequestDispatcher,
- //指定传递页面,通过forward传递参数出去
- request.setAttribute("name",usernameString);
- request.setAttribute("pwd",passwordString);
- request.getRequestDispatcher("/loginok.jsp").forward(request,response);
- }
-
-
- public void destroy() {
- super.destroy(); // Just puts "destroy" string in log
- // Put your code here
- }
-
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
-
- }
-
-
-
- /**
- * Initialization of the servlet.
- *
- * @throws ServletException if an error occurs
- */
- public void init() throws ServletException {
- // Put your code here
- }
-
- }
功能:前端交互页面,接收用户输入的用户名、密码信息
注:在from的action中填写ServletCkde的请求地址(包含项目名+路由地址)

-
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head>
- <body>
- <form action="/1109/servlet/ServletCkde" method="post">
- 姓名:<input type="text"name="username"><br>
- 密码:<input type="text"name="password"><br>
- <input type="submit"name="提交">
- </form>
- </body>
- </html>
运行一下页面效果
功能:从servlet的接收用户名和密码并提示欢迎信息,并在五秒后跳转到index.jsp首页

- <%@ 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>
- <body>
- <%
- String strname=(String)request.getAttribute("name");
- String strpwd=(String)request.getAttribute("pwd");
- %>
- <p style="color:red">
- <%
- response.setHeader("refresh","2;ur1=/Webbk/index.jsp");
- %>
- 欢迎你:<%=strname%>你已成功登录,请牢记您的密码<%=strpwd%>三秒后跳转到首页</p>
- 秒后跳转到首页,如没有跳转你可以点击<ahref="index.jsp">首页〈/a>跳转到首页
- </body>
- </html>

- <%@ 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 'index.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>
- 这是首页 <br>
- </body>
- </html>


