文章中用到的环境: java17 Tocat10 mysql-connector-java-8.0.27.jar
1.开发第一个servlet
Servlet规范: 第一步: 在webapps下创建一个目录,名为:crm(当然也可以为其他的,例如银行为bank,办公系统为oa等等) 第二步: 在webapp的根下创建一个WEB-INF目录 第三步: 在WEB-INF下创建一个目录:classes 第四步: 在WEB-INF目录下创建一个目录:lib(这个目录不是必须的,但如果一个webapp需要第三方jar包的话,这个jar包就要放到这个lib目录下) 第五步: 在WEB-INF下创建一个web.xml的配置文件,配置文件内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="https://jakarta.ee/xml/ns/jakartaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee
https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd"
version="5.0"
metadata-complete="true">
</web-app>
Servlet的完整包名在tomcat10版本上是 jakarta.servlet.Servlet 低版本的才是javax.servlet.Servlet jarkataServlet的5个方法:
HelloServlet.java
package com.xy.servlet;
import jakarta.servlet.Servlet;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import jakarta.servlet.ServletConfig;
import java.io.IOException;
import java.io.PrintWriter;
public class HelloServlet implements Servlet{
public void init(ServletConfig config) throws ServletException{
}
public void service(ServletRequest request,ServletResponse response)
throws ServletException,IOException{
System.out.println("This is my first servlet!");
PrintWriter out = response.getWriter();
out.print("Hello,This is my first servlet!");
}
public void destroy() {
}
public String getServletInfo() {
return "";
}
public ServletConfig getServletConfig() {
return null;
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="https://jakarta.ee/xml/ns/jakartaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee
https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd"
version="5.0"
metadata-complete="true">
<servlet>
<servlet-name>firstservlet</servlet-name>
<servlet-class>com.xy.servlet.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>firstservlet</servlet-name>
<url-pattern>/first</url-pattern>
</servlet-mapping>
</web-app>
之后将HelloServlet.java放入classes文件夹中,使用javac -d . HelloServlet.java 进行编译,即可得到HelloServlet.class文件
目录结构
- webapps
- crm
- WEB-INF
- classes
- lib
- web.xml
- index.html
笔记总结
向浏览器响应一段HTML代码
public void service(ServletRequest request,ServletResponse response){
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.print("<h1>hello,this is my servlet!</h1>");
}
2.JDBC连接数据库
首先创建一个数据库
create database JDBC;
创建表
create table t_student(ID int,name varchar(255));
向表中插入数据
insert into t_student values(1,"zhangsan");
insert into t_student values(2,"lisi");
insert into t_student values(3,"wangwu");
创建StudentServlet.java
package com.xy.servlet;
import jakarta.servlet.Servlet;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import jakarta.servlet.ServletConfig;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
public class StudentServlet implements Servlet{
public void init(ServletConfig config) throws ServletException{
}
public void service(ServletRequest request,ServletResponse response)
throws ServletException,IOException{
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try{
Class.forName("com.mysql.cj.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/jdbc";
String user = "root";
String password = "root";
conn = DriverManager.getConnection(url,user,password);
String sql = "select ID,name from t_student";
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while(rs.next()){
String ID = rs.getString("ID");
String name = rs.getString("name");
out.print(ID + "," + name);
}
}catch(Exception e){
e.printStackTrace();
}finally{
if(rs !=null){
try{
rs.close();
}catch(Exception e){
e.printStackTrace();
}
}
if(ps !=null){
try{
ps.close();
}catch(Exception e){
e.printStackTrace();
}
}
if(conn !=null){
try{
conn.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
}
public void destroy() {
}
public String getServletInfo() {
return "";
}
public ServletConfig getServletConfig() {
return null;
}
}
|