tags:

views:

120

answers:

3

enter code hereHiya, so far I didn't use JSP at all and I find some online tutorials to be very complicated to learn from them, I need to make simple login form .. its not problem for me to do it in PHP but in JSP ist a big problem. Here is how I'd do it in PHP plain and simple, HTML :

<form action="action.php" method="post">
<input name="username" type="text" />
<input name="password" type="password" />
</form>

Here is action.php

<?php
$user = $_POST['username'];
$pwd = $_POST['password'];
if($user != "" && $pwd != "")
{
  ... check whether md5 value of $pwd for username $user matches the value in database..
if so then redirect somewhere..
}
?>

How would I do something like this in JSP

A: 

There lots of examples everywhere on the internet: ...

Kai
yea sure, I've discovered google long time ago , but as I said its really hard and more complex than php thats why I needed some clarification, if I was able to google/figure it out on my own, I wouldn't be bothering couple of guys I don't even know now would I?I think your answer is not really an answer it belongs in the comments . thank you
Gandalf StormCrow
@Gandalf: Yes, you're right, shoud have been a comment.
Kai
+2  A: 

Unlike PHP, you normally use JSP for presentation only. Despite the fact you can write Java code in a JSP file using scriptlets (the <% %> things with raw Java code inside), you should be using (either directly or indirectly) a servlet class to control requests and execute business logic. You can use taglibs in JSP to generate/control the output. A standard taglib is JSTL, with the JSTL 'core' being the most important. You can use EL (Expression Language) to access data which is available in page, request, session and application scopes.

To start, create a JSP file which contains basically the following:

<form action="login" method="post">
    <input type="text" name="username">
    <input type="password" name="password">
    <input type="submit">
</form>

Then create a servlet class which has the doPost() as follows implemented:

String username = request.getParameter("username");
String password = request.getParameter("password");
User user = userDAO.find(username, password);
if (user != null) {
    request.getSession().setAttribute("user", user); // Logged in!
    response.sendRedirect("home"); // Home page?
} else {
    response.sendRedirect("error"); // Error page? You can eventually redisplay same JSP with error message.
}

Finally map this servlet in web.xml like:

<servlet>
    <servlet-name>loginServlet</servlet-name>
    <servlet-class>myservlets.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>loginServlet</servlet-name>
    <url-pattern>/login</url-pattern>
</servlet-mapping>

Which means that you can invoke it by http://example.com/webapp/login as done in <form action>.

When learning JSP/Servlet I recommend the following resources:
JSP/Servlet tutorial at sun.com.
JSP/Servlet tutorials at coreservlets.com.
Head First Servlets & JSP.

Good luck.

BalusC
Thank you for your example I'll now take some time to read all that.
Gandalf StormCrow
You're welcome. If you want more technical assist, ask.
BalusC
+1  A: 

It's a little more involved in Java using old school Servlets and JSP than in PHP. You need to create a servlet which can access the HttpRequest and HttpResponse objects and read out the parameters you sent with the JSP file to that Servlet using the GET or POST method. Furthermore you have to create web descriptor file called web.xml and deploy everything in a servlet container such as Tomcat.

I would start googling for "hello world servlet", once you get the idea of it works it shouldn't be difficult anymore.

nkr1pt