码迷,mamicode.com
首页 > 编程语言 > 详细

JavaWeb学习

时间:2019-07-17 00:36:32      阅读:117      评论:0      收藏:0      [点我收藏+]

标签:png   sql   tco   容器   odi   传递   package   dea   远程   

---恢复内容开始---

Reponse(需要掌握)

重定向:将这个请求,转换到另一个地址

//通过响应对象重定向页面
resp.sendRedirect("404.jsp");

ServletContext

  • web容器在启动时,他会为每个web应用程序都创建一个对应的ServletContext对象,他代表当前web应用。
  • 多个Servlet通过ServletContext对象实现数据共享
  • 获取web应用的初始化参数
  • 用ServletContexthi实现请求转发
  • 利用ServletContext对象读取资源文件

例一,通过ServletContext对象读取网站配置文件

1,新建一个database.properties文件

driver=com.mysql.jdbc.Driver
username=root
password=123456
url=jdbc:mysql://localhost:3306/smbms

2,编写servlet类

package com.kuang.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

//读取properties配置文件
public class ServletDemo03 extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //读取配置文件

        //1.获得配置文件的路径
        String realPath = this.getServletContext().getRealPath("/WEB-INF/classes/resources/database.properties");
        System.out.println("取得的路径为:"+realPath);

        Properties properties = new Properties();
        FileInputStream is = new FileInputStream(realPath);

        properties.load(is);//把文件流加载到配置文件的对象中;

        String driver = properties.getProperty("driver");
        String username = properties.getProperty("username");
        String password = properties.getProperty("password");
        String url = properties.getProperty("url");

        //响应到网页
        resp.getWriter().println(driver);
        resp.getWriter().println(username);
        resp.getWriter().println(password);
        resp.getWriter().println(url);

        //=======================================
        System.out.println(driver);
        System.out.println(username);
        System.out.println(password);
        System.out.println(url);
    }
}

3,配置web.xml

<servlet>
    <servlet-name>Demo03</servlet-name>
    <servlet-class>com.kuang.servlet.ServletDemo03</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>Demo03</servlet-name>
    <url-pattern>/q3</url-pattern>
</servlet-mapping>

4,网页打开

locallhost:8080/demo02/q3

5,网页显示

技术图片

Request(需要掌握)

JSP取得项目的路径

${pageContext.request.contextPath}

Request对象

package com.kuang.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


public class RequestDemo01 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //查看request对象的方式
        System.out.println(request.getContextPath()); //获得web项目路径
        System.out.println(request.getRequestURI()); //请求的URL路径
        //Remote 远程
        System.out.println(request.getRemoteUser()); //获得远程的用户名
        System.out.println(request.getRequestedSessionId()); //获得SessionId;
        System.out.println(request.getServletPath()); //请求servlet的url
        System.out.println(request.getLocalAddr()); //获得本地地址  防火墙
        System.out.println(request.getLocalName()); //获得本地名字
        System.out.println(request.getLocalPort()); //获得访问的端口号

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}

IDEA运行结果

技术图片

Request对象接受前端用户提交的参数

1,写前端注册页面

  • 表单的提交方式get/post
  • 表单提交的位置action:服务器的路径/servlet对应的请求url

 

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>注册</title>
</head>
<body>

<%--
JSP的注释
--%>

<%--
Form表单
    action:代表要提交的路径,表单要提交到哪里 ,可以提交到一个Servlet
        //提交到Servlet需要,获取到Web项目的路径下的Servlet
    method: 提交表单的方式
--%>
<h1>注册</h1>

<form action="${pageContext.request.contextPath}/r2" method="post">

    <p>用户名:<input type="text" name="username"></p>
    <p>密码:<input type="password" name="password"></p>

    <p>
        <input type="submit">
        <input type="reset">
    </p>

</form>

</body>
</html>

2,写处理前端提交信息的Servlet

  • 接收前端传递的控件信息,request.getParameter(“控件的name”)

 

 1 package com.kuang.servlet;
 2 import javax.print.attribute.HashPrintRequestAttributeSet;
 3 import javax.servlet.ServletException;
 4 import javax.servlet.http.HttpServlet;
 5 import javax.servlet.http.HttpServletRequest;
 6 import javax.servlet.http.HttpServletResponse;
 7 import java.io.IOException;
 8 
 9 //处理前端提交过来的数据
10 public class RequestDemo02 extends HttpServlet {
11     @Override
12     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
13         doPost(req,resp);
14     }
15     @Override
16     protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
17 
18         //通过HttpServletRequest获取前端用户提交的信息
19         //req.getParameterValues(); 获得前端传递的控件信息,通过控件的name;但是可以接受多个参数的控件,比如(多选框....)
20         //req.getParameter(); 获得前端传递的控件信息,通过控件的name;
21 
22         req.setCharacterEncoding("utf-8");
23         resp.setCharacterEncoding("utf-8");
24 
25         String username = req.getParameter("username");
26         String password = req.getParameter("password");
27 
28         //连接数据库,比较数据库中的信息是否匹配
29 
30         if (username.equals("admin")&&password.equals("1234567")){
31             System.out.println("登录成功!"); //跳转到成功页面
32         }else {
33             System.out.println("登录失败!"); //提示重新注册
34         }
35 
36         System.out.println(username);
37         System.out.println(password);
38     }
39 }

3,配置web.xml

   配置对应的XML文件

 <servlet>
        <servlet-name>RequsetDemo02</servlet-name>
        <servlet-class>com.kuang.servlet.RequestDemo02</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>RequsetDemo02</servlet-name>
        <url-pattern>/r2</url-pattern>
    </servlet-mapping>

 

 

---恢复内容结束---

JavaWeb学习

标签:png   sql   tco   容器   odi   传递   package   dea   远程   

原文地址:https://www.cnblogs.com/like-lry/p/11198219.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!