关注微信公众号:新方圆小棉袄,下载免费看电影的app
访问Servlet的方式介绍
服务器中转发访问Servlet
浏览器地址栏输入URL访问Servlet
重定向访问Servlet
jsp对应的网页上发出请求(重点)
超链接访问
window.location.href访问
伪连接
事件
表单提交(get,post)
理其它客户端访问
下面介绍浏览器地址栏输入URL访问Servlet的方法
代码如下:
一:TestServlet.java
package com.fs.test1;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.fs.test.TestService;
import com.fs.test.TestServiceInf;
@WebServlet("/test.do")
public class TestServlet extends HttpServlet {
private TestService testService = new TestServiceInf();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//http://localhost:8080/web15_visitorServletWithParm3/test.do?username=wgr&pwd=123
//接收请求参数 username, pwd
String username = req.getParameter("username").trim();
String pwd = req.getParameter("pwd").trim();
//登陆
boolean flag = testService.login(username, pwd);
if(flag == true) {//登陆成功
//转发到main.jsp
req.getRequestDispatcher("/main.jsp").forward(req, resp);
}else {//登陆失败
//转发到error.jsp
req.getRequestDispatcher("/error.jsp").forward(req, resp);
}
}
}
二:TestService.java
package com.fs.test;
public interface TestService {
public boolean login(String uesrname,String pwd);
}
三:TestServiceInfS.java
package com.fs.test;
public class TestServiceInf implements TestService{
@Override
public boolean login(String username, String pwd) {
/*如果username和pwd正确返回true,否则返回false*/
if("wgr".equals(username) && "123".equals(pwd)){
return true;
}
return false;
}
此处省略main.jsp和error.jsp两个简单的jsp代码,自己可写
四:在浏览器中输入:http://localhost:8080/Java7.6/test.do?username=wgr&&pwd=123
在实际应用中此方法可用于黑客跳过网站js验证,直接进入网站。
微信公众号:新方圆小棉袄