前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >JavaWeb入门之Servlet小练习

JavaWeb入门之Servlet小练习

原创
作者头像
黄桂期
发布2018-05-16 17:27:55
1.1K5
发布2018-05-16 17:27:55
举报
文章被收录于专栏:土豆专栏土豆专栏

写给还是小白的我们,一起加油!

今天来一个巨简单的Servlet小练习,顺带温习下前两天的内容~

练习:

在web.xml文件中设置两个WEB应用的初始化参数,username,password,创建一个html页面,定义两个请求字段并发送到一个Servlet中,对应web.xml中的参数是否一样;若一致,响应hello:username;若失败则Sorry:username

html:

代码语言:javascript
复制
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <form action="PracticeServlet" method="post">
    username:<input type="text" name="username"/>
    password:<input type="password" name="password"/>
    <input type="submit" value="Submit"/>
    </form>
</body>
</html>

PracticeServlet:

代码语言:javascript
复制
package per.huang.pra;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class PracticeServlet implements Servlet {

    @Override
    public void destroy() {
        // TODO Auto-generated method stub
        
    }

    @Override
    public ServletConfig getServletConfig() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public String getServletInfo() {
        // TODO Auto-generated method stub
        return null;
    }
    private ServletConfig servletConfig;
    @Override
    public void init(ServletConfig servletConfig) throws ServletException {
        // TODO Auto-generated method stub
        this.servletConfig=servletConfig;
    }

    @Override
    public void service(ServletRequest request, ServletResponse response)
            throws ServletException, IOException {
        //1,获取请求参数:username,password
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        //2,获取当前WEB应用的初始化参数:user,password
        //需要使用ServletContext对象
        ServletContext servletContext=servletConfig.getServletContext();
        String initUser=servletContext.getInitParameter("user");
        String initPassword=servletContext.getInitParameter("password");
        PrintWriter out=response.getWriter();
        //3,比对
        //4,打印响应字符串
        if(initUser.equals(username)&&initPassword.equals(password)){
            out.print("Hello:"+username);
        }else{
            out.print("Sorry"+username);;
        }
        
        
    }

}

web.xml:

代码语言:html
复制
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xmlns="http://java.sun.com/xml/ns/javaee" 
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
         id="WebApp_ID" version="3.0">
  <display-name>Day0516</display-name>
  <!-- 配置当前WEB应用的初始化参数 -->
  <context-param>
      <param-name>user</param-name>
      <param-value>huang</param-value>
  </context-param>
  <context-param>
      <param-name>password</param-name>
      <param-value>123456</param-value>
  </context-param>
  <!-- 配置Servlet -->
  <servlet>
      <servlet-name>PracticeServlet</servlet-name>
      <servlet-class>per.huang.pra.PracticeServlet</servlet-class>
  </servlet>
  <servlet-mapping>
      <servlet-name>PracticeServlet</servlet-name>
      <url-pattern>/PracticeServlet</url-pattern><!-- 和跳转的action后面的内容一样 -->
  </servlet-mapping>
</web-app>

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档