JAVAEE 核心API Servlet

JAVAEE 核心API Servlet

步骤:
1.继承HttpServlet
2.重写doGet()或doPost()方法
 2.1.在方法内获取发送过来的请求参数,方法(req.getParameter())
 2.2.进行业务处理
 2.3.服务器向浏览器进行响应
3.在web.xml中配置url

package com.book.servlet;

import java.io.IOException;

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 com.book.operator.BookOperator;

public class AddBookServlet extends HttpServlet{
	@Override
//	重写doPost()方法
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//		设置请求字符集为utf-8		
		req.setCharacterEncoding("utf-8");
//		获得一个图书的操作类对象,里面有对图书的增删改查方法
		BookOperator bop = new BookOperator();
		
//		获取从浏览器发送来的请求参数
		String name = req.getParameter("name");
		String author = req.getParameter("author");
		String press = req.getParameter("press");
		String count = req.getParameter("count");
		String price = req.getParameter("price");
		String status = req.getParameter("status");
		String time = req.getParameter("time");
//		将获取到的参数放在object数组中,通过上面的操作类对象把这个数组对象作为参数传输过去,获取一个int类型的结果。这个servlet的方法的意思是,如果添加图书成功则返回1,否则返回0
		Object [] objs = {name , author , press , count , price , status , time};
		int i = bop.addbook(objs);
		
//		设置响应内容类型编码,防止响应内容乱码
		resp.setContentType("text/html;charset=utf-8");
		if (i > 0) {
//			如果添加成功,则重定向到findall界面		
			resp.getWriter().write("添加图书成功");
			resp.sendRedirect("findall");
		}else {
			resp.getWriter().write("添加图书失败");
		}
	}
}

	<servlet>
//		给这个类创建一个对象,	
		<servlet-name>addbook</servlet-name>
//		这个为servlet类,运行在服务器上的java代码
		<servlet-class>com.book.servlet.AddBookServlet</servlet-class>
	</servlet>
	<servlet-mapping>
//		这个类创建的对象对应一个虚拟路径
		<servlet-name>addbook</servlet-name>
//		此处为虚拟路径,浏览器可以通过这个路径找到对应的servlet类
		<url-pattern>/addbook</url-pattern>
	</servlet-mapping>
Java EE 5 Platform Packages javax.activation The JavaBeans(TM) Activation Framework is used by the JavaMail(TM) API to manage MIME data. javax.annotation javax.annotation.security javax.ejb The javax.ejb package contains the Enterprise JavaBeans classes and interfaces that define the contracts between the enterprise bean and its clients and between the enterprise bean and the EJB container. javax.ejb.spi The javax.ejb.spi package defines interfaces that are implemented by the EJB container. javax.el Provides the API for the Unified Expression Language shared by the JSP 2.1 and JSF 1.2 technologies. javax.enterprise.deploy.model Provides Tool Vendor implementation classes. javax.enterprise.deploy.model.exceptions Provides Tool Vendor exception implementation classes. javax.enterprise.deploy.shared Provides shared objects for Tool Vendor and Product Vendor implementation classes. javax.enterprise.deploy.shared.factories Provides shared factory manager object for Tool Vendor and Product Vendor implementation classes. javax.enterprise.deploy.spi Provides J2EE Product Vendor implementation classes. javax.enterprise.deploy.spi.exceptions Provides J2EE Product Vendor deployment exception implementation classes. javax.enterprise.deploy.spi.factories Provides J2EE Product Vendor deployment factory implementation classes. javax.enterprise.deploy.spi.status Provides J2EE Product Vendor deployment status implementation classes. javax.faces Top level classes for the JavaServer(tm) Faces API. javax.faces.application APIs that are used to link an application's business logic objects to JavaServer Faces, as well as convenient pluggable mechanisms to manage the execution of an application that is based on JavaServer Faces. javax.faces.component Fundamental APIs for user interface components. javax.faces.component.html Specialized user interface component classes for HTML. javax.faces.context Classes and interfaces defining per-request state information. javax.faces.convert Contains classes and interfaces defining converters. javax.faces.el DEPRECATED Classes and interfaces for evaluating and processing reference expressions. javax.faces.event Interfaces describing events and event listeners, and concrete event implementation classes. javax.faces.lifecycle Classes and interfaces defining lifecycle management for the JavaServer Faces implementation. javax.faces.model Standard model data beans for JavaServer Faces. javax.faces.render Classes and interfaces defining the rendering model. javax.faces.validator Interface defining the validator model, and concrete validator implementation classes. javax.faces.webapp Classes required for integration of JavaServer Faces into web applications, including a standard servlet, base classes for JSP custom component tags, and concrete tag implementations for core tags. javax.interceptor The javax.interceptor package contains classes and interfaces for use with EJB interceptors. javax.jms The Java Message Service (JMS) API provides a common way for Java programs to create, send, receive and read an enterprise messaging system's messages. javax.jws javax.jws.soap javax.mail The JavaMailTM API provides classes that model a mail system. javax.mail.event Listeners and events for the JavaMail API. javax.mail.internet Classes specific to Internet mail systems. javax.mail.search Message search terms for the JavaMail API. javax.mail.util Utility classes. javax.management.j2ee Provides the J2EE Management Enterprise Bean component (MEJB) interfaces. javax.management.j2ee.statistics Provides the standard interfaces for accessing performance data from J2EE managed objects Package Specification JSR 77, J2EE Management Related Documentation For overviews, tutorials, examples, guides, and tool documentation, please see: J2EE Tools javax.persistence The javax.persistence package contains the classes and interfaces that define the contracts between a persistence provider and the managed classes and the clients of the Java Persistence API. javax.persistence.spi The javax.persistence.spi package defines the classes and interfaces that are implemented by the persistence provider and the Java EE container for use by the container, provider, and/or Persistence bootstrap class in deployment and bootstrapping.
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值