Servlet与jdbc的开发(idea版) 基础

  • jdbc 开发与介绍

servlet 引言

		Servlet(Server Applet)是Java Servlet的简称,称为小服务程序或服务连接器,
	用Java编写的服务器端程序,具有独立于平台和协议的特性,主要功能在于交互式地浏览和生成数据,生成动态Web内容。
	狭义的Servlet是指Java语言实现的一个接口,广义的Servlet是指任何实现了这个Servlet接口的类,一般情况下,人们将Servlet理解为后者。Servlet运行于支持Java的应用服务器中。从原理上讲,Servlet可以响应任何类型的请求,但绝大多数情况下Servlet只用来扩展基于HTTP协议的Web服务器。

servlet 开发

注意:当前环境是离线开发

开发环境

idea+servlet+oracle+html

目录结构

Servlet与jdbc的开发(idea版) 基础_第1张图片

创建一个file 文件(普通文件)

Servlet与jdbc的开发(idea版) 基础_第2张图片

Java+servlet 结构

Servlet与jdbc的开发(idea版) 基础_第3张图片

servlet 结构

Servlet与jdbc的开发(idea版) 基础_第4张图片

静态资源的目录结构

Servlet与jdbc的开发(idea版) 基础_第5张图片

javaEE 与servlet开发 (正文开始)

编程思路(小白推荐)

  • 将jdbc与数据库连接(注意:这里使用Oracle)
    1.1 jdbc开发
  • servlet 的配置与页面

servlet 开发

1.一个类继承 httpServlet 覆盖service方法

例子1 展示所有 servlet

ShowServlet (第一个java与servlet练习)

package com.baizhi.servlet;

import javax.servlet.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

//这个类继承servlet类,实现五个方法 
//最重要的是service方法
public class ShowServlet implements Servlet {
    @Override
    public void init(ServletConfig servletConfig) throws ServletException {

    }

    @Override
    public ServletConfig getServletConfig() {
        return null;
    }

    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        servletResponse.setContentType("text/html");
        servletResponse.setCharacterEncoding("utf-8");
        PrintWriter writer = servletResponse.getWriter();
        writer.print("");
        writer.print("

" + new Date() +"中国热"+"

"); writer.print(""); } @Override public String getServletInfo() { return null; } @Override public void destroy() { } }
package com.baizhi.servlet;

import com.baizhi.dao.UserDAO;
import com.baizhi.daoImpl.UserDAOImpl;
import com.baizhi.entity.User;

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

public class ShowAll extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置编码格式
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        //调用dao
        UserDAO userDAO = new UserDAOImpl();
        List<User> userList = userDAO.queryAll();
        //解析响应
        resp.setContentType("text/html");
        //判断跳转页面
        HttpSession session = req.getSession();
        User login = (User) session.getAttribute("login");
        if (login != null) {
            if (userList != null) {
                req.setAttribute("showAll", userList);
                req.getRequestDispatcher("/ShowAllView").forward(req, resp);
            } else {
                resp.sendRedirect("/JDBC/user/login.html");
            }
        } else {
            resp.sendRedirect("/JDBC/user/login.html");

        }

    }
}

展示所有的动态视图(原始的输出流)

package com.baizhi.servletview;

import com.baizhi.entity.User;

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

public class ShowAllView extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        List login = (List) req.getAttribute("showAll");
        PrintWriter writer = resp.getWriter();
        if (login != null) {
            writer.print("");
            writer.print("
"); writer.print(""); writer.print(""); writer.print(""); writer.print(""); writer.print(""); writer.print(""); writer.print(""); for (User user : login) { writer.print(""); writer.print(""); writer.print(""); writer.print(""); writer.print(""); writer.print(""); } writer.print("
" + "姓名" + "" + "密码" + "" + "删除" + "" + "修改" + "
" + user.getUsername() + "" + user.getPassword() + "" + user.getId() + "" + user.getId() + "
"); writer.print(""); writer.print(""); writer.print(""); } else { writer.print(""); writer.print("

" + "展示失败" + "

"); writer.print(""); } } }

web.xml 配置文件的书写


<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_4_0.xsd"
         version="4.0">
    
    <servlet>
        <servlet-name>ShowServletservlet-name>
        <servlet-class>com.baizhi.servlet.ShowServletservlet-class>
        
        <load-on-startup>1load-on-startup>
    servlet>
    <servlet-mapping>
        <servlet-name>ShowServletservlet-name>
        <url-pattern>/ShowServleturl-pattern>
    servlet-mapping>

    
    <servlet>
        <servlet-name>ParameterServletservlet-name>
        <servlet-class>com.baizhi.servlet.ParameterServletservlet-class>
        <load-on-startup>1load-on-startup>
    servlet>
    <servlet-mapping>
        <servlet-name>ParameterServletservlet-name>
        <url-pattern>/ParameterServleturl-pattern>
    servlet-mapping>
    
    <servlet>
        <servlet-name>LoginServletViewservlet-name>
        <servlet-class>com.baizhi.servletview.LoginServletViewservlet-class>
        <load-on-startup>1load-on-startup>
    servlet>
    <servlet-mapping>
        <servlet-name>LoginServletViewservlet-name>
        <url-pattern>/LoginServletViewurl-pattern>
    servlet-mapping>
    
    <servlet>
        <servlet-name>ShowAllservlet-name>
        <servlet-class>com.baizhi.servlet.ShowAllservlet-class>
        <load-on-startup>1load-on-startup>
    servlet>
    <servlet-mapping>
        <servlet-name>ShowAllservlet-name>
        <url-pattern>/ShowAllurl-pattern>
    servlet-mapping>
    
    <servlet>
        <servlet-name>ShowAllViewservlet-name>
        <servlet-class>com.baizhi.servletview.ShowAllViewservlet-class>
        <load-on-startup>1load-on-startup>
    servlet>
    <servlet-mapping>
        <servlet-name>ShowAllViewservlet-name>
        <url-pattern>/ShowAllViewurl-pattern>
    servlet-mapping>
    
    <servlet>
        <servlet-name>ValidationCodeServletservlet-name>
        <servlet-class>com.baizhi.servlet.ValidationCodeServletservlet-class>
        <load-on-startup>1load-on-startup>
    servlet>
    <servlet-mapping>
        <servlet-name>ValidationCodeServletservlet-name>
        <url-pattern>/ValidationCodeServleturl-pattern>
    servlet-mapping>
    
    <servlet>
        <servlet-name>AddServletservlet-name>
        <servlet-class>com.baizhi.servlet.AddServletservlet-class>
        <load-on-startup>1load-on-startup>
    servlet>
    <servlet-mapping>
        <servlet-name>AddServletservlet-name>
        <url-pattern>/AddServleturl-pattern>
    servlet-mapping>
    
    <servlet>
        <servlet-name>DeleteServletservlet-name>
        <servlet-class>com.baizhi.servlet.DeleteServletservlet-class>
        <load-on-startup>1load-on-startup>
    servlet>
    <servlet-mapping>
        <servlet-name>DeleteServletservlet-name>
        <url-pattern>/DeleteServleturl-pattern>
    servlet-mapping>

web-app>

结数语

首先欢迎大家阅读这篇文章,这里的例子我给的有点少,我觉得不影响开发。
servlet基础开发到这里就结束了,大家有建议就提出来,我在后期做优化,感谢大家观看。

你可能感兴趣的:(servlet,开发)