1、什么是Servlet:Servlet是运行在服务端,用来响应客户端请求的对象,这种对象称之为Servlet。
1.1、创建Servlet
网站目录结构:
第一个Servlet程序
java
package servlet;
import java .io.*;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.WebServlet;
@WebServlet(name="firstServlet",urlPatterns={"/firstServlet"})
public class FirstServlet extends HttpServlet
{
private static final long serialVersionUID=1L;
public void init(ServletConfig config) throws ServletException{
super.init(config);
}
public void service(HttpServletRequest request,HttpServletResponse response) throws IOException{
response.setContentType("text/html;charset=utf8");
PrintWriter out =response.getWriter();
out.print("<html><body>");
out.print("第一个servlet对象");
out.print("</html></body>");
}
}
编译Servlet 命令:javac -encoding utf8 -cp web-inf/lib/servlet-api.jar; -d web-inf/classes servlet/FirstServlet.java
1.2使用web.xml部署servlet
1.3使用注解类WebServlet部署servlet
运行结果:
解决前面Servlet编译器报红问题:
练习:
网站目录结构:
Computer.java
java
package servlet;
import java.io.*;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.WebServlet;
@WebServlet(urlPatterns = {"/computer"})
public class Computer extends HttpServlet{
public void init(ServletConfig config) throws ServletException{
super.init(config);
}
public void service(HttpServletRequest request,HttpServletResponse response)throws IOException{
response.setContentType("text/html;charset=utf8");
PrintWriter out=response.getWriter();
out.print("<html><body>");
String length = request.getParameter("length");
String width = request.getParameter("width");
double area = 0;
try{
double length1=Double.parseDouble(length);
double width1=Double.parseDouble(width);
if(length1>=0&&width1>=0){
out.print("<br>长是"+length1+"宽是"+width1+"矩形的面积:");
out.print("<br>"+length1*width1);
}else{
out.print("<br>矩形的长和宽不能是负数");
}
}catch(NumberFormatException e){
out.print("<H1>请输入数字字符</H1>");
}
out.println("</body></html>");
}
}
编译Servlet 命令:javac -encoding utf8 -cp web-inf/lib/servlet-api.jar; -d web-inf/classes servlet/Computer.java
example5_1.jsp
java
<%@page language="java" contentType="text/html;charset=utf8"%>
<html>
<body>
<font size="3">
<form action="computer" method="post">
<br>请输入矩形的长:
<input type ="text" name="length"/>
<br>请输入矩形的宽:
<input type ="text" name="width"/>
<br>
<input type="submit" value="提交"/>
</form>
</font>
</body>
</html>
运行结果:
ShowLetter.java
java
package servlet;
import java.io.*;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.WebServlet;
@WebServlet(urlPatterns = {"/letter"})
public class ShowLetter extends HttpServlet {
public void init(ServletConfig config) throws ServletException{
super.init(config);
}
public void service(HttpServletRequest request,HttpServletResponse response)throws IOException{
response.setContentType("text/html;charset=utf8");
PrintWriter out=response.getWriter();
out.print("<html><body>");
out.print("<br>小写字母:");
for(char c='a';c<='z';c++){
out.print(" "+c);
}
out.print("<br>大写字母:");
for(char c='A';c<='Z';c++){
out.print(" "+c);
}
out.println("</body></html>");
}
}
编译Servlet 命令:javac -encoding utf8 -cp web-inf/lib/servlet-api.jar; -d web-inf/classes servlet/ShowLetter.java
example5_2.jsp
java
<%@page language="java" contentType="text/html;charset=utf8"%>
<html>
<body>
<font size=3>
单击超链接查看英语字母表:
<br><a href="letter">查看英语字母表</a>
</font>
</body>
</html>
运行结果: