实验目的
- 编写Servlet代码;
- 熟悉并掌握Servlet的使用和配置。
实验内容
【1】利用Servlet实现一个简单的登录系统,要求:
- 包括登录页面、登录成功页面和登录失败提示页面;
- 用户可以在登录页面输入用户名和密码;
- 点击登录页面中的提交按钮,并在Servlet对输入的信息进行判断;
- 如果用户名错误,则在登录失败页面显示“用户名不存在”;
- 如果用户名正确,但密码错误,则显示“密码错误,请重新输入”;
- 如果用户名和密码都正确,则进入登录成功页面。
Servlet
package gdpu.com;import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;public class Test10 extends HttpServlet {/*** @see HttpServlet#HttpServlet()*/public Test10() {super();// TODO Auto-generated constructor stub}/*** @see Servlet#init(ServletConfig)*/public void init(ServletConfig config) throws ServletException {// TODO Auto-generated method stub}/*** @see Servlet#destroy()*/public void destroy() {// TODO Auto-generated method stub}/*** @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)*/protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stub}/*** @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)*/protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stubrequest.setCharacterEncoding("UTF-8");response.setCharacterEncoding("UTF-8");response.addHeader("content-type", "text/html;charset=utf-8");String username = request.getParameter("username");String password = request.getParameter("password");if("gdpu".equals(username)&&"123".equals(password)){request.setCharacterEncoding("UTF-8");response.getWriter().print("登录成功");}else if("gdpu".equals(username)){request.setCharacterEncoding("UTF-8");response.getWriter().print("密码错误,请重新输入");}else{request.setCharacterEncoding("UTF-8");response.getWriter().print("用户名不存在");}}}
Login
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<html>
<head>
<meta charset=UTF-8>
<title>GDPU官网登录页面</title>
</head>
<%String usernamex = (String)session.getAttribute("login");if(usernamex == null){}else{response.sendRedirect("./index1.jsp");}
%>
<body>
<form action ="./login.html" method ="post">GDPU账号:<input type = "text" style = "width220px;" name = "username"/><br><br>账号密码 :<input type = "password" style = "width220px;" name = "password"/><br><br><input type = "submit" style = "width:220px;" value = "登录"/>
</form>
</body>
</html>
【2】利用Servlet实现一个天气预报信息的API,具体可预报的城市和对应的天气情况,可以运用数组进行模拟。
package gdpu.com;import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;public class Test10 extends HttpServlet {/*** @see HttpServlet#HttpServlet()*/public Test10() {super();// TODO Auto-generated constructor stub}/*** @see Servlet#init(ServletConfig)*/public void init(ServletConfig config) throws ServletException {// TODO Auto-generated method stub}/*** @see Servlet#destroy()*/public void destroy() {// TODO Auto-generated method stub}/*** @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)*/protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stubrequest.setCharacterEncoding("UTF-8");response.setCharacterEncoding("UTF-8");response.addHeader("content-type", "text/html;charset=utf-8");String city[]=new String[] {"北京","上海","广州","深圳"};String num = request.getParameter("num");int numx = Integer.parseInt(num);String weather[]=new String[] {"小雪","多云","晴","小雨"};response.getWriter().print(city[numx]+"今天天气"+weather[numx]);}/*** @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)*/protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stub}}