servlet+jsp+java实现Web 应用

servlet+jsp+java实现Web 应用

  用java来构建一个web应用是特别容易的事情,jsp和php很像,可以嵌套在html中。程序的结构很简单,也很清楚,本文主要记录下大概的开发过程和环境的搭建。

环境搭建

下载:

  1. eclipse 
  2. tomcat
  3. eclipse tomcat 插件

开发过程

1.建立一个Dynamic Web Project

2.创建一个欢迎页面

  页面可以是jsp/html,我们选择一个jsp页面(放在WebContent内)

复制代码
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>Do you come in?</h1>
<form method="post" action="hello.do">
    Select:<br>
    <select>
        <option>yes
        <option>no
    </select>
    <center>
        <input type="submit">
    </center>
</form>
</body>
</html>
复制代码

2.向工程添加一个servlet文件

复制代码
package com.example;

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

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 Welcome
 */
@WebServlet("/Welcome")
public class Welcome extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        String c = request.getParameter("select");
        if(c.equals("yes"))
            out.print("Welcome!");
        else
            out.print("I don't like you!");
    }

}
复制代码

3.创建一个web.xml

  web.xml用来建立servlet与jsp的关系(需要放在WEB-INF内)。

  根据不同的url来调用不同的servlet来进行处理。

复制代码
<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
  <display-name></display-name>
  <servlet>
    <servlet-name>Welcome</servlet-name>//要与下面的名称相同
    <servlet-class>com.example.Welcome</servlet-class>//调用的类的位置
   </servlet>
   <servlet-mapping>
    <servlet-name>Welcome</servlet-name>
    <url-pattern>/hello.do</url-pattern>//url标识
  </servlet-mapping>
</web-app>
复制代码

什么是MVC

  MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写。其实上面的结构就是一种MVC,页面用jsp来展现,控制用servlet,而模型就是用普通的JAVA类来实现不同的处理过程。

posted @   cococo点点  阅读(49917)  评论(4编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 单线程的Redis速度为什么快?
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 展开说说关于C#中ORM框架的用法!
· SQL Server 2025 AI相关能力初探
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
历史上的今天:
2012-10-26 getchar()和EOF
点击右上角即可分享
微信分享提示