jsp和servlet学习总结

一.Jsp与servlet的区别:

jsp是java代码嵌入html中,用java代码控制来html。

Servlet完全是JAVA程序代码构成,用来流程控制和事务处理

jsp更擅长表现于页面显示,servlet更擅长于逻辑控制.

Servlet中没有内置对象,Jsp中的内置对象都是必须通过HttpServletRequest对象,HttpServletResponse对象以及HttpServlet对象得到.

二、如何配置一个servlet使它运行

1.servlet的名字,包类

<servlet>

<servlet-name>httpServletDemo(为servlet取的名字)</servlet-name>

<servlet-class>com.lxitedu.HttpServletDemo(包名.类名)</servlet-class>

</servlet>

2.映射.访问的地址(url)

<servlet-mapping>

<servlet-name>httpServletDemo(与上面的servlet名字要完全一样)</servlet-name>

<url-pattern>/myServlet(运行时地址栏显示的文件名)</url-pattern>

</servlet-mapping>

三、servlet跳转方法

1).使用form表单的action属性设置要跳转的页面(此方法可以将此页面的form表单属性值传递到下一页面):如下

<form action="my.jsp" name="lili" onsubmit="return test()">

<input type="submit" value="提交">

或者(为button类型需要手动提交表单):

<input type="button" value="注册" onclick="test()">

javascript的函数:

function test(){

    //手动设置跳转页面

     document.lili.action="my1.jsp";

    document.lili.submit();

}

2).使用javascript中的页面跳转的方法(不能将form表单中的属性值传递到下一页面):

window.location.href="my.jsp";

window.location.replace("my.jsq");//此方法不可将页面后退.

2.服务端跳转的两种方法对比:

<jsp:forward page="xxx.jsp"/>:等价于

request.getRequestDispatcher("xxx.jsp").forward(request,response);

本服务器的资源跳转,效率更高.地址栏不改变(仍为跳转前的页面).可得到request属性值.

response.sendRedirect("xxx.jsp"):

重定向到任意资源.地址栏改为当前页面.无法得到request属性值.

原文地址:https://www.cnblogs.com/hq233/p/6295135.html