Servlet
1、什么是Servlet:Servlet是运行在服务端,用来响应客户端请求的对象,这种对象称之为Servlet。
1.1、创建Servlet
网站目录结构:
第一个Servlet程序
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
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
<%@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
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
<%@page language="java" contentType="text/html;charset=utf8"%>
<html>
<body>
<font size=3>
单击超链接查看英语字母表:
<br><a href="letter">查看英语字母表</a>
</font>
</body>
</html>
运行结果:
原文地址:https://blog.csdn.net/m0_68041576/article/details/137647584
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!