javaWeb之 Servlet的应用(结合jsp页面)

it2022-05-05  220

Servlet在开发过程中就是结合Jsp页面来应用的,下面我们就来用一个例子来简单的实现一下,里面主要包含了Servlet中方法的用法。

jsp页面

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <%=request.getAttribute("name")%> <form action="LoginServlet" method="post">//action中填的是Web.xml中的映射地址 <table> <tr> <td>用户名</td> <td><input type="text" name="username"/></td> </tr> <tr> <td>密码</td> <td><input type="password" name="userpass"/></td> </tr> <tr> <td>性别</td> <td><input type="radio" name="sex" value="男"/><input type="radio" name="sex" value="女"/></td> </tr> <tr> <td>爱好</td> <td><input type ="checkbox" name="hobby" value="篮球"/>篮球 <input type ="checkbox" name="hobby" value="足球"/>足球 <input type ="checkbox" name="hobby" value="排球"/>排球 <input type ="checkbox" name="hobby" value="唱歌"/>唱歌</td> </tr><tr> <td><input type="submit" name="submit" value ="登录"/></td> <td></td> </tr> </table> </form> </body> </html>

web.xml文件

<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>WebFirstDemo</display-name> <context-param> <param-name>javaWeb</param-name> <param-value>哼哼的博客</param-value> </context-param> <servlet> <description>xml文件的编写</description> <display-name>LoginServlet</display-name> <servlet-name>LoginServlet</servlet-name> <servlet-class>cn.bzu.mywebproject.servlet.LoginServlet</servlet-class> <init-param> <param-name>username</param-name> <param-value>hengheng</param-value> </init-param> <init-param> <param-name>userpass</param-name> <param-value>123456</param-value> </init-param> <load-on-startup>0</load-on-startup> </servlet> <servlet-mapping> <servlet-name>LoginServlet</servlet-name> <url-pattern>/LoginServlet</url-pattern> </servlet-mapping> </web-app>

Servlet的实现类

package cn.bzu.mywebproject.servlet; import java.io.IOException; import java.util.Enumeration; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class LoginServlet */ public class LoginServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } // protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //设置响应的编码方式 response.setCharacterEncoding("UTF-8"); //接受的编码方式 request.setCharacterEncoding("utf-8"); //获得提交的方式 String method = request.getMethod(); //输出到页面中 response.getWriter().write("请求的方法是"+method); System.out.println("请求的方法是"+method); //获得请求资源所属的Web应用的途径 String path = request.getContextPath(); response.getWriter().write("web的路径是"+path); System.out.println("web的路径是"+path); //获得Servlet的映射路径 String mappingUrl = request.getServletPath(); response.getWriter().write("web的映射路径是"+mappingUrl); System.out.println("web的路径是"+mappingUrl); //获得Servlet的名字 String ServletName= request.getServerName(); response.getWriter().write("web的路径是"+ServletName); //获得所有用户的请求参数 Enumeration<String> enumeration = request.getParameterNames(); while (enumeration.hasMoreElements()) { String string = (String) enumeration.nextElement(); response.getWriter().write(string); System.out.println("用户的所有的请求参数是"+string); } //获得用户的指定请求参数 String name = request.getParameter("username"); response.getWriter().write("用户名得值是"+name); System.out.println("用户名得值是"+name); String pass = request.getParameter("userpass"); response.getWriter().write("密码得值是"+pass); System.out.println("密码得值是"+pass); String sex = request.getParameter("sex"); response.getWriter().write("性别得值是"+sex); System.out.println("性别得值是"+sex); //获得求参数的一组值 String hobbys [] = request.getParameterValues("hobby"); for(String hobby : hobbys) { response.getWriter().write("用户的爱好是"+hobby); System.out.println("用户的爱好是"+hobby); } //设置request域中的属性值 request.setAttribute("name", name);//这里的值只能造转发中能接受到,重定向后就接受不到 //转发 //request.getRequestDispatcher(request.getContextPath()+"/NewFile.jsp").forward(request, response); //重定向,跳转到这个页面 response.sendRedirect(request.getContextPath()+"/NewFile.jsp"); } }

页面的效果图

输出结果

请求的方法是POST web的路径是/MyWebProject web的路径是/LoginServlet 用户的所有的请求参数是username 用户的所有的请求参数是userpass 用户的所有的请求参数是sex 用户的所有的请求参数是hobby 用户的所有的请求参数是submit 用户名得值是哼哼的博客 密码得值是123456 性别得值是男 用户的爱好是篮球 用户的爱好是排球

最新回复(0)