servlet在整个JSP中充当什么角色?服务器实行JSP是把JSP文件转化为servlet对吗?

coyisoso 2004-01-09 07:02:23
小弟初学,高手指点!
...全文
67 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
newman0708 2004-01-10
  • 打赏
  • 举报
回复
jsp是做为前台运行的,而servlet是适合做后台运行。

jsp无需手动编译,由tomcat来处理,而servlet必须要编译。

当我们实现MVC模型时,View可以由jsp或者servlet来充当,
而Control则是由servlet来充当,
xqi8 2004-01-10
  • 打赏
  • 举报
回复
不错。
cocolovevienna 2004-01-10
  • 打赏
  • 举报
回复
jsp在执行时都会编译成servlet的,这个不错.
楼上的兄弟们说的都不错,MVC也好,实现逻辑功能也好,都是servlet的优点.
JavaHobby1216 2004-01-10
  • 打赏
  • 举报
回复
我的观点可能有些不同,我认为Servlet在MVC模式下是作为一个转发器来使用的,它的作用
主要是处理一些前台静态页面如HTML,JSP发出的验证请求,如果验证通过,则转发到下一个页面。服务器执行JSP页面时,其实是执行编译好的.CLASS文件。再通过Servlet把结果传递回来,客户端看到的其实是一个执行结果。

phsam 2004-01-09
  • 打赏
  • 举报
回复
正如楼上两位所说的。

呵呵~~
stonecsdn 2004-01-09
  • 打赏
  • 举报
回复
servlet一般充当控制的角色,或者只有很少的要给客户端显示的代码。
jsp要先经过编译才能执行,在编译之前,要先解析jsp成为xml格式,然后产生一个中间格式的java文件,最后才编译这个java文件,你可以看看,类似下面,有些近似servlet:
先看看源文件吧:
<%@ page contentType="text/html; charset=GBK" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title> JavaMail - 发送HTML邮件</title>
</head>
<body>
<table border="0" cellspacing="0" cellpadding="0">
<form method="post" action="testFormJsp.jsp">
<tr>
<td>smtp主机:</td>
<td><input type="text" name="smtpHost" size="40"></td>
</tr>
<tr>
<td>发信人:</td>
<td><input type="text" name="from" size="40"></td>
</tr>
<tr>
<td>收信人:</td>
<td><input type="text" name="to" size="40"></td>
</tr>
<tr>
<td>抄送人:</td>
<td><input type="text" name="cc" size="40"></td>
</tr>
<tr>
<td>暗送人:</td>
<td><input type="text" name="bcc" size="40"></td>
</tr>
<tr>
<td>主题:</td>
<td><input type="text" name="subject" size="40"></td>
</tr>
<tr>
<td>附件:</td>
<td><input type="file" name="file" size="40" ></td>
</tr>
<tr>
<td valign="top">内容:</td>
<td><textarea name="content" rows="5" cols="80"></textarea></td>
</tr>
<tr>
<td colspan=2 align="center">
<input type="submit" value="发送">
  
<input type="reset" value="重置">
</td>
</tr>
</form>
</table>
</body>
</html>
产生一个中间格式的java文件:
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import org.apache.jasper.runtime.*;

public class form_0002ejsp_jsp extends HttpJspBase {


private static java.util.Vector _jspx_includes;

public java.util.List getIncludes() {
return _jspx_includes;
}

public void _jspService(HttpServletRequest request, HttpServletResponse response)
throws java.io.IOException, ServletException {

JspFactory _jspxFactory = null;
javax.servlet.jsp.PageContext pageContext = null;
HttpSession session = null;
ServletContext application = null;
ServletConfig config = null;
JspWriter out = null;
Object page = this;
JspWriter _jspx_out = null;


try {
_jspxFactory = JspFactory.getDefaultFactory();
response.setContentType("text/html; charset=GBK");
pageContext = _jspxFactory.getPageContext(this, request, response,
null, true, 8192, true);
application = pageContext.getServletContext();
config = pageContext.getServletConfig();
session = pageContext.getSession();
out = pageContext.getOut();
_jspx_out = out;

out.write("\r\n");
out.write("<html>\r\n");
out.write("<head>\r\n");
out.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\">\r\n");
out.write("<title> JavaMail - 鍙戦€丠TML閭欢");
out.write("</title>\r\n");
out.write("</head>\r\n");
out.write("<body>\r\n");
out.write("<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\r\n");
out.write("<form method=\"post\" action=\"testFormJsp.jsp\">\r\n");
out.write("<tr>\r\n");
out.write("<td>smtp涓绘満锛?);
out.write("</td>\r\n");
out.write("<td>");
out.write("<input type=\"text\" name=\"smtpHost\" size=\"40\">");
out.write("</td>\r\n");
out.write("</tr>\r\n");
out.write("<tr>\r\n");
out.write("<td>鍙戜俊浜猴細");
out.write("</td>\r\n");
out.write("<td>");
out.write("<input type=\"text\" name=\"from\" size=\"40\">");
out.write("</td>\r\n");
out.write("</tr>\r\n");
out.write("<tr>\r\n");
out.write("<td>鏀朵俊浜猴細");
out.write("</td>\r\n");
out.write("<td>");
out.write("<input type=\"text\" name=\"to\" size=\"40\">");
out.write("</td>\r\n");
out.write("</tr>\r\n");
out.write("<tr>\r\n");
out.write("<td>鎶勯€佷汉锛?);
out.write("</td>\r\n");
out.write("<td>");
out.write("<input type=\"text\" name=\"cc\" size=\"40\">");
out.write("</td>\r\n");
out.write("</tr>\r\n");
out.write("<tr>\r\n");
out.write("<td>鏆楅€佷汉锛?);
out.write("</td>\r\n");
out.write("<td>");
out.write("<input type=\"text\" name=\"bcc\" size=\"40\">");
out.write("</td>\r\n");
out.write("</tr>\r\n");
out.write("<tr>\r\n");
out.write("<td>涓婚锛?);
out.write("</td>\r\n");
out.write("<td>");
out.write("<input type=\"text\" name=\"subject\" size=\"40\">");
out.write("</td>\r\n");
out.write("</tr>\r\n");
out.write("<tr>\r\n");
out.write("<td>闄勪欢锛?);
out.write("</td>\r\n");
out.write("<td>");
out.write("<input type=\"file\" name=\"file\" size=\"40\" >");
out.write("</td>\r\n");
out.write("</tr>\r\n");
out.write("<tr>\r\n");
out.write("<td valign=\"top\">鍐呭锛?);
out.write("</td>\r\n");
out.write("<td>");
out.write("<textarea name=\"content\" rows=\"5\" cols=\"80\">");
out.write("</textarea>");
out.write("</td>\r\n");
out.write("</tr>\r\n");
out.write("<tr>\r\n");
out.write("<td colspan=2 align=\"center\">\r\n");
out.write("<input type=\"submit\" value=\"鍙戦€乗">\r\n  \r\n");
out.write("<input type=\"reset\" value=\"閲嶇疆\">\r\n");
out.write("</td>\r\n");
out.write("</tr>\r\n");
out.write("</form>\r\n");
out.write("</table>\r\n");
out.write("</body>\r\n");
out.write("</html>");
} catch (Throwable t) {
out = _jspx_out;
if (out != null && out.getBufferSize() != 0)
out.clearBuffer();
if (pageContext != null) pageContext.handlePageException(t);
} finally {
if (_jspxFactory != null) _jspxFactory.releasePageContext(pageContext);
}
}
}
Leemaasn 2004-01-09
  • 打赏
  • 举报
回复
有一个古老的传说,
有一个勇敢的父亲Sun,
想让他的儿子J2EE能更好地与别人交流,
于是,教给了他一门技术Servlet。
但是,这个技术并不是那么好掌握,
于是,爸爸就用Jsp来画了张图,
来表示Servlet,

所以,当他用Servlet描述(编程)比较困难的时候,
他就可以改用Jsp来描述了。


Jsp可以看作是对Servlet的封装,
执行Jsp前会先把它翻译成Servlet,
然后再执行!

81,115

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧