jsp能写servlet类 吗 jsp页面当servlet类?
我做一个二次开发,我在想jsp不是能嵌套java代码吗,所以想在jsp页面写servlet类 但是不知道怎么调用,
(不要告诉我写在java里面,因为不行我才写到jsp页面)
在web.xml配置 通过
<servlet>
<servlet-name>testServlet</servlet-name>
<jsp-file>/jsp/test.jsp</jsp-file>
</servlet>
确实能访问到test.jsp 但是不会不调用doGet() doPost方法
--以下是test.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="com.esoon.cisiss.common.BaseServlet" %>
<%@ page import="java.io.IOException" %>
<%@ page import="javax.servlet.ServletException" %>
<%@ page import="javax.servlet.http.HttpServletRequest" %>
<%@ page import="javax.servlet.http.HttpServletResponse" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<script type="text/javascript">
alert("");
</script>
<%
class testServlet extends BaseServlet{
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.print("----------1-------");
System.out.print("--------2---------");
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
}
%>
[解决办法]
一个jsp写doPost干吗,想做什么直接做,想接值直接写request.getParameter
[解决办法]
其实可以在jsp中使用
<%!
public void method(){
}
%>
来定义全局方法提供调用就好了,,
另外 我尝试了使用楼主的方法,但是好像不行,每次都是执行jsp的_jspService ,,设置了url-pattern 亦如此。
我看编译文件终于发现了:
这个servlet是定义在方法内的,直接调用肯定行不通。。
具体我也不会。。
[解决办法]
同意5楼啊~具体什么原因我也没试过,感觉配置文件这么写可能有问题吧~
感觉没什么实用价值啊~在jsp里添加java代码本来就很乱,再写servlet恐怕连执行效率都会降低啊~
[解决办法]
jsp最终会编译成一个servlet类,jsp偏向于表示层,servlet注重逻辑,这个你或许试试这个jsp编译成servlet的名字叫啥,在调用。 你这种具体我也没试过! 求楼下大神解释吧!
[解决办法]
public final class login_jsp extends org.apache.jasper.runtime.HttpJspBase
implements org.apache.jasper.runtime.JspSourceDependent {
private static final JspFactory _jspxFactory = JspFactory.getDefaultFactory();
private static java.util.List _jspx_dependants;
private javax.el.ExpressionFactory _el_expressionfactory;
private org.apache.AnnotationProcessor _jsp_annotationprocessor;
public Object getDependants() {
return _jspx_dependants;
}
public void _jspInit() {
_el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();
_jsp_annotationprocessor = (org.apache.AnnotationProcessor) getServletConfig().getServletContext().getAttribute(org.apache.AnnotationProcessor.class.getName());
}
public void _jspDestroy() {
}
public void _jspService(HttpServletRequest request, HttpServletResponse response)
throws java.io.IOException, ServletException {
PageContext pageContext = null;
HttpSession session = null;
ServletContext application = null;
ServletConfig config = null;
JspWriter out = null;
Object page = this;
JspWriter _jspx_out = null;
PageContext _jspx_page_context = null;
try {
response.setContentType("text/html;charset=UTF-8");
pageContext = _jspxFactory.getPageContext(this, request, response,
null, true, 8192, true);
_jspx_page_context = pageContext;
application = pageContext.getServletContext();
config = pageContext.getServletConfig();
session = pageContext.getSession();
out = pageContext.getOut();
_jspx_out = out;
out.write("\r\n");
out.write("\r\n");
out.write("\r\n");
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
basePath = basePath +"";
out.write("\r\n");
out.write("<html>\r\n");
out.write("\t<head>\r\n");
out.write("\t\t<meta http-equiv="pragma" content="no-cache">\r\n");
out.write("\t\t<meta http-equiv="cache-control" content="no-cache">\r\n");
out.write("\t\t<title>欢迎访问");


[解决办法]
不知道我理解的对不,LZ只是想写一个ajax请求,但是不能写JAVA类,只能修改jsp。
$.ajax({
url : "/brokermark/aaa.jsp",
type:'post',
context : this,
data : {"ddd" : "111"},
error : function(jqXHR, textStatus, errorThrown) {
alert();
},
success : function(data, textStatus, jqXHR) {
alert(data);
}
});
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
String ddd = request.getParameter("ddd");
out.print(ddd+"====");
%>