我使用Java Servlets创建了一个Web系统,现在想要进行JUnit测试.我dataManager只是将它提交到数据库的基本代码.你会如何使用JUnit测试Servlet?
我的代码示例允许用户注册/注册,这是通过AJAX从我的主页面提交的:
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
// Get parameters
String userName = request.getParameter("username");
String password = request.getParameter("password");
String name = request.getParameter("name");
try {
// Load the database driver
Class.forName("com.mysql.jdbc.Driver");
//pass reg details to datamanager
dataManager = new DataManager();
//store result as string
String result = dataManager.register(userName, password, name);
//set response to html + no cache
response.setContentType("text/html");
response.setHeader("Cache-Control", "no-cache");
//send response with register result
response.getWriter().write(result);
} catch(Exception e){
System.out.println("Exception is :" + e); …Run Code Online (Sandbox Code Playgroud) 我正在尝试将列表中的元素打印到新行上,但我无法将其工作;
printElements :: [String] -> IO()
printElements (x:xs) = print x (some kind of newline, then loop?) printElements xs
Run Code Online (Sandbox Code Playgroud)
所以这:
["1","2","2","4"]
Run Code Online (Sandbox Code Playgroud)
会给:
1
2
3
4
Run Code Online (Sandbox Code Playgroud) 我试图理解下面的代码,其中b给定的整数image是一个图像.
据我所知,如果给定点i,j的RGB值大于b,则将该像素设置为白色,否则设置为黑色.所以会将图像转换为黑白图像.
但是我迷失了什么(&0xff)实际上,我猜它是一种二元转换?
if ((image.getRGB(i, j) & 0xff) > b) {
image.setRGB(i, j, 0xffffff) ;
} else {
image.setRGB(i, j, 0x000000);
}
Run Code Online (Sandbox Code Playgroud) 我有一个非常简单的问题:给定一个函数接受一个char并返回一个字符串
test :: Char -> [String]
Run Code Online (Sandbox Code Playgroud)
如何将char转换为字符串?我对这两种类型感到困惑.
我试图在不使用cURL的情况下将数据从一个页面转发到另一个页面,这可能吗?
目前我已经尝试过了
header('HTTP/1.1 307 Temporary Redirect');
header('Location: new-location.php');
Run Code Online (Sandbox Code Playgroud)
这很好用,但是给出了一个愚蠢的弹出框,任何其他方法?
我尝试过使用curl但没有任何反应,不确定它是否在我的服务器上启用了!
如何删除字符串的每个第n个元素?
我猜你会drop以某种方式使用这个功能.
像这样丢弃第一个n,你怎么能改变这个,所以只丢下第n个,然后是第n个,等等,而不是全部?
dropthem n xs = drop n xs
Run Code Online (Sandbox Code Playgroud) 我刚刚使用Django-Rest-Framework创建了我的第一个基于令牌的Web API,它对我的移动应用程序非常有效.
我即将开始创建一个基于Django的网站,但我想尽可能多地重用我的API代码.
我能看到的选项是:
你通常如何使用Django处理这种代码重用?
我正在尝试使用servlet创建一个上传表单,到目前为止,我有以下内容;
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if (isMultipart) {
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
try {
List items = upload.parseRequest(request);
Iterator iterator = items.iterator();
while (iterator.hasNext()) {
FileItem item = (FileItem) iterator.next();
if (!item.isFormField()) {
String fileName = item.getName();
String root = getServletContext().getRealPath("");
File path = new File(root + "/uploads");
if (!path.exists()) {
boolean status = path.mkdirs();
}
File uploadedFile = new File(path + "/" + …Run Code Online (Sandbox Code Playgroud) 我想多次调用一个函数,每次都为其提供不同的输入,但是我试图确保在前一个函数调用完成后触发下一个函数调用。
例如:
func(1,2);
func(9,3);
func(6,4);
func(5,6);
Run Code Online (Sandbox Code Playgroud)
我见过回调的编码如下;
function1(someVariable, function() {
function2(someOtherVariable);
});
Run Code Online (Sandbox Code Playgroud)
但这仅在调用第二个函数时才有效,我尝试链接函数,但这只允许我运行一定数量的函数。
有没有办法可以对同一函数应用某种回调?
注意::该函数只是添加一个类。
func(x, y){
$("#block").addClass("class");
setTimeout(function(){
$("#block").removeClass("class");
},1000);
};
Run Code Online (Sandbox Code Playgroud) haskell ×4
java ×2
django ×1
generics ×1
javascript ×1
jquery ×1
junit ×1
php ×1
polymorphism ×1
servlets ×1
testing ×1
unit-testing ×1